序列化程序django rest框架上的字段值属性错误

Posted

技术标签:

【中文标题】序列化程序django rest框架上的字段值属性错误【英文标题】:attribute error on value for field on serializer django rest framework 【发布时间】:2018-08-07 20:19:15 【问题描述】:

我收到以下错误:

AttributeError: Got AttributeError when attempting to get a value for field `city` on serializer `SearchCitySerializer`

我的序列化器是正确的,除非我明显遗漏了什么。

这是我的模型:

class SearchCity(models.Model):
    city = models.CharField(max_length=200)

这是我的序列化器

class SearchCitySerializer(serializers.ModelSerializer):
    class Meta:
        model = SearchCity
        fields = ('pk','city')

*** 我在字段中尝试了没有 pk 的序列化程序,但仍然失败

这里在视图中使用:

 from serializers import SearchCitySerializer

 def get(self, request, format=None):
        searchcityqueryset = SearchCity.objects.all()
        serializedsearchcity = SearchCitySerializer(searchcityqueryset)

        return Response(
            'searchcity': serializedsearchcity.data,
        )

我得到的完整错误:

File "/home/rickus/Documents/softwareProjects/211hospitality/suitsandtables/backend/virtualsuits/suitsandtables/suitsandtablessettingsapp/views.py", line 37, in get
    'searchcity': serializedsearchcity.data,
  File "/home/rickus/Documents/softwareProjects/211hospitality/suitsandtables/backend/virtualsuits/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 537, in data
    ret = super(Serializer, self).data
  File "/home/rickus/Documents/softwareProjects/211hospitality/suitsandtables/backend/virtualsuits/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 262, in data
    self._data = self.to_representation(self.instance)
  File "/home/rickus/Documents/softwareProjects/211hospitality/suitsandtables/backend/virtualsuits/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 491, in to_representation
    attribute = field.get_attribute(instance)
  File "/home/rickus/Documents/softwareProjects/211hospitality/suitsandtables/backend/virtualsuits/local/lib/python2.7/site-packages/rest_framework/fields.py", line 462, in get_attribute
    raise type(exc)(msg)
AttributeError: Got AttributeError when attempting to get a value for field `city` on serializer `SearchCitySerializer`.
The serializer field might be named incorrectly and not match any attribute or key on the `QuerySet` instance.
Original exception text was: 'QuerySet' object has no attribute 'city'.
[28/Feb/2018 02:41:43] "GET /api/dependancy/suitsadmin/settings/ HTTP/1.1" 500 20823

数据库目前是空的,因为数据库中根本没有数据

【问题讨论】:

【参考方案1】:
serializedsearchcity = SearchCitySerializer(searchcityqueryset)

改成

serializedsearchcity = SearchCitySerializer(searchcityqueryset, many=True)

要序列化查询集或对象列表而不是单个对象实例,您应该在实例化序列化程序时传递many=True 标志。然后,您可以传递要序列化的查询集或对象列表。

Dealing with multiple objects

【讨论】:

以上是关于序列化程序django rest框架上的字段值属性错误的主要内容,如果未能解决你的问题,请参考以下文章

Django rest框架 - 模型序列化程序不会序列化所有字段

django rest 框架:从序列化器 validate() 方法设置字段级错误

如何获取主键相关字段嵌套序列化器django rest框架的所有值

Django Rest 框架 ListField 和 DictField

如何在 django rest 框架中为枚举字段创建序列化程序

如何从序列化器字段订购 django rest 框架查询集?