question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Multiple objects returned while serializing using cities__name_std

See original GitHub issue

I have serializer like this: `class ExperienceSerializer(serializers.ModelSerializer):

    experience_type = ChoicesField(choices=Experience.EXPERIENCE_TYPE)
    location = serializers.SlugRelatedField(slug_field='name_std', queryset=City.objects.all())

`

Whenever I enter “Delhi” as location It fails with “get() returned more than one City – it returned 2!” There are cities with same name so how to hanle this scenario?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
blagcommented, Dec 27, 2016

I just pushed version 0.5 to PyPI, so you should be able to upgrade using pip:

pip install --upgrade django-cities

That version adds slugs to models, and allows you to specify your own slugify() function for all django-cities objects if you don’t want to use the default slugify function I wrote.

Since all of the building blocks are in place, and the default settings should handle this (or at least handle 90% of it), I’m closing this issue. Feel free to reopen it if you run into any issues. Cheers!

2reactions
blagcommented, Jul 28, 2016

You will need to use the slug field for this - that’s what it’s there for after all!

    location = serializers.SlugRelatedField(slug_field='slug', queryset=City.objects.all())

Keep in mind that you will need to fill in that field yourself, but…

I’ve got some changes I need to push, and one of them is creating a unique constraint on the Place (abstract) model’s slug field and giving each model a slugify() method. That should allow you to use the slug field to uniquely identify each city. I’ll try to get that pushed either later today or Friday for you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Multiple Objects Returned - django - Stack Overflow
I want a all subject data according to course which I select..When I hit this api if single data present working awesome but...
Read more >
7780 (get returns multiple objects when model includes a ...
Django throws a MultipleObjectsReturned error when getting a single instance of a model that includes a ManyToManyField in the default ordering.
Read more >
Django Rest Framework API #22 / Many To Many ... - YouTube
Django Rest Framework API Model RelationshipsHow to implement ManyToMany Relationship to link multiple objects of one model with multiple ...
Read more >
Custom object creation - Optimizely
This topic describes how to customize instantiation of objects returned in search results in Optimizely Search & Navigation.
Read more >
returned correct array with multiple objects, but - Esri Community
Hello All, I have a simple method to get portal users by a role and put them in an array and return the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found