django rest框架中的django_countries [英] django_countries in django rest framework

查看:82
本文介绍了django rest框架中的django_countries的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个可以使用django_countries返回所有国家/地区的API。

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",)


推荐答案

您可以在django_countries中使用serializer_fields。

You can user serializer_fields in django_countries.

就像这样:

from django_countries.serializer_fields import CountryField

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

    class Meta:
        model = models.Person
        fields = ('name', 'email', 'country')

这篇关于django rest框架中的django_countries的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆