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",)
niemmi
  • 17,113
  • 7
  • 35
  • 42
Mark Waugh
  • 423
  • 1
  • 5
  • 10

1 Answers1

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')
Kishan Mehta
  • 2,598
  • 5
  • 39
  • 61