django_countries in django rest framework
Asked Answered
T

1

3

I am trying to create an API which can return all the countries using django_countries.

I am trying something following but it is not working. As an individual field, it works fine but with complete countries list it is giving an error.

from django_countries import countries


class CountrySerializer(serializers.Serializer):

    country = serializers.ListField(source=countries)
    class Meta:
    fields = ("country",)
Torsk answered 18/11, 2016 at 4:20 Comment(2)
What is the error,show us your code and full traceback of the error.Riesman
Exception Type: AttributeError Exception Value: 'Countries' object has no attribute 'split' Exception Location:Torsk
D
4

You can user serializer_fields in django_countries.

Like This:

from django_countries.serializer_fields import CountryField

class PersonSerializer(serializers.ModelSerializer):
    country = CountryField()

    class Meta:
        model = models.Person
        fields = ('name', 'email', 'country')
Declinature answered 18/11, 2016 at 8:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.