django-countries: Field is not serializeable with Django Rest Framework
Hello,
I have encountered an issues when working with the country field and Django Rest Framwork. If in the models we have a field that can be set to blank:
country = CountryField(_('country'), blank=True, null=True, default='NL')
when used as a normal field in the serializer I get an error that: TypeError: Country(code=u’') is not JSON serializable. This happens only when the country field is blank.
It has been reported by someone else also on the django rest framework group. https://groups.google.com/forum/#!topic/django-rest-framework/3hrS2xr6BS0
I am not sure exactly if this can be solved on the django rest framework side but thought to report it here also. It might be solved on the django-countries side.
Thanks, Vlad
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 30 (8 by maintainers)
Commits related to this issue
- Don't make a Country object ever equal anything if it's empty Potentially a fix for #106 — committed to SmileyChris/django-countries by SmileyChris 9 years ago
@SmileyChris I had the same issue as @rschwiebert did and @jgeerds suggestion helped.
I was using:
from django_countries.fields import CountryFieldWhen I should have been using:from django_countries.serializer_fields import CountryFieldI think it would be helpful to add the correct import statement needed in the the code section of “Django Rest Framework field” on the readme as it wasn’t obvious since it’s the same name as the Django field.
@rschwiebert It works for me. Are you sure you’re using the right serializer?
I think the country field is translated into a serializers.ChoiceField. It works well if i have a country, but if it is not then it gets the error.
How is solved it for my case: I have a CompanySerializer and i overwrote the country field with a custom serializer. The default one would have been a serializers.ChoiceField:
In the to_representaion method of the SerializableCountryField i returned and empty string instead of the normal ‘value’ variable. The value in that case is Countru(u’') which is not serializable.
I hope this helped?
this error come in multi-select countries. to fix it just add
CountryFieldMixinin the serializer eg:class MySerializer(CountryFieldMixin, serializers.ModelSerializer)Hello!
I found that this
from django_countries.serializer_fields import CountryFielddoesn’t work withCountryField(multiple=True)because “to_representation” method doesn’t work with lists. So I wrote my own class:Am I right or misunderstood somethign?
Forgot to say that I need code+name presentation