Django REST Framework:object不仅在一个属性上,而不是在其他属性上

2024-09-28 01:28:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直很难理解为什么传入field_to_native()的对象(obj)在我更改属性后会从Nonetype更改为正确的对象。。。在

这是原版:Splitting model instance for serializer into 3 different fields

stackoverflow的mariodev在最初的问题上帮助了我,但是一个奇怪的bug我们都搞不懂:

下面是它似乎有问题的代码:

COORD = dict(x=0, y=1, z=2)
class CoordField(serializers.Field):
    def field_to_native(self, obj, field_name):
        #retrieve and split coords
        coor = obj.xyz.split('x')
        return int(coor[COORD[field_name]])

class NoteSerializer(serializers.ModelSerializer):
    owner = serializers.Field(source='owner.username')
    firstname = serializers.Field(source='owner.first_name')
    lastname = serializers.Field(source='owner.last_name')
    x = CoordField()
    y = CoordField()
    z = CoordField()


class Meta:
    model = Note
    fields = ('id','owner','firstname','lastname','text','color','time','x', 'y', 'z')

xyz是模型注释中的一个实例。根据回溯,当我这么做的时候目标xyz,目标=无。在

奇怪的是,如果我这么做的话目标颜色,obj返回正确(注:someuser)

我不明白obj是如何通过改变属性来改变的。在

我无法理解的是,JSON数据'x''y'和'z'正在传递给视图,而我的div框获得正确的左、上和z索引CSS数据。如果这行得通,为什么我会出错?在

如果有错误,为什么数据仍能通过?在

^{pr2}$

你可以看到,穿过y轴。在

任何启示都将是美妙的!多谢了!在

以下是文本视图中的回溯:

Traceback:
File "/home1/thecupno/python2.7/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  140.                     response = response.render()
File "/home1/thecupno/python2.7/lib/python2.7/site-packages/django/template/response.py" in render
  105.             self.content = self.rendered_content
File "/home1/thecupno/python2.7/lib/python2.7/site-packages/rest_framework/response.py" in rendered_content
  59.         ret = renderer.render(self.data, media_type, context)
File "/home1/thecupno/python2.7/lib/python2.7/site-packages/rest_framework/renderers.py" in render
  582.         post_form = self.get_rendered_html_form(view, 'POST', request)
File "/home1/thecupno/python2.7/lib/python2.7/site-packages/rest_framework/renderers.py" in get_rendered_html_form
  485.             data = serializer.data
File "/home1/thecupno/python2.7/lib/python2.7/site-packages/rest_framework/serializers.py" in data
  510.                 self._data = self.to_native(obj)
File "/home1/thecupno/python2.7/lib/python2.7/site-packages/rest_framework/serializers.py" in to_native
  309.             value = field.field_to_native(obj, field_name)
File "/home1/thecupno/django/notes/desk/serializers.py" in field_to_native
  10.         coor = obj.xyz#.split('x')      <-- I commented out .split, and the problem still persists.
Exception Type: AttributeError at /desk/restnote/
Exception Value: 'NoneType' object has no attribute 'xyz'

Tags: tonameinpyselfobjfieldlib
1条回答
网友
1楼 · 发布于 2024-09-28 01:28:55

我已经解决了我自己的问题。 解决方案是确保序列化程序在对象为None时返回None。在

下面是顶部代码部分的对应关系:

COORD = dict(x=0, y=1, z=2)
class CoordField(serializers.Field):
def field_to_native(self, obj, field_name):
    if obj is None:
        return None
    else:
        #retrieve and split coords
        coor = obj.xyz.split('x')
        return int(coor[COORD[field_name]])

希望这能帮助Django程序员们!在

相关问题 更多 >

    热门问题