简体   繁体   English

覆盖 Django REST Framework 中的 serializer.data

[英]Override serializer.data in Django REST Framework

I've been trying to alter the value of a form field the Django REST Framework's admin panel and for some reason the change never takes place.我一直在尝试更改 Django REST Framework 管理面板中表单字段的值,但由于某种原因,更改从未发生。 I have the serializer below我有下面的序列化程序

class SomeView(ModelViewSet):
  queryset = MyModel.objects.all()
  serializer_class = MyModelSerializer

  # I Want to override this and change the POST data
  def perform_create(self, serializer):
     user = self.request.user.id

     # this was a form field where I manually entered the user ID
     # I now want it to default to the logged in user
     serializer.data['user'] = user

     # This returns the original user that was entered into the form field
     print serializer.data

I checked out serializer.data with dir() and it's just a python dictionary so I can't figure out why I can't modify the value.我用dir()检查了 serializer.data ,它只是一个 python 字典,所以我不知道为什么我不能修改这个值。 As a test, I tried to add extra values but that doesn't work either作为测试,我尝试添加额外的值,但这也不起作用

# this doesnt work
serializer.data['some_new_field'] = 'test'

EDIT编辑

On another note, I can copy the data and edit it另一方面,我可以复制数据并进行编辑

fake_data = serializer.data.copy()
fake_data['old_value'] = 'new value'

However, it always fails to validate但是,它总是无法验证

serializer = MyModelSerializer(data=fake_data)
serializer.is_valid() # returns false

EDIT EDIT:编辑 编辑:

Ok, so the validation error was caused by Django returning a SimpleLazyObject.好的,所以验证错误是由 Django 返回 SimpleLazyObject 引起的。 Everything works now when I perform a copy of the data, but I'm really curious as to why I can't edit serializer.data directly without copying it.当我执行数据副本时,现在一切正常,但我真的很好奇为什么我不能直接编辑serializer.data而不复制它。 The problem is solved now, but if anyone can provide insight on the issue just for curiosity, that would be awesome.问题现在已经解决了,但如果有人能出于好奇就对这个问题提供见解,那就太棒了。

I checked out serializer.data with dir() and it's just a python dictionary so I can't figure out why I can't modify the value.我用 dir() 检查了 serializer.data ,它只是一个 python 字典,所以我不知道为什么我不能修改这个值。

While the value returned from Serializer.data is indeed a dictionary, Serializer.data is not a simple instance variable.虽然从Serializer.data返回的值确实是一个字典,但Serializer.data不是一个简单的实例变量。

If you look at rest_framework/serializers.py :如果您查看rest_framework/serializers.py

class Serializer(BaseSerializer, metaclass=SerializerMetaclass):
    # [...]
    @property
    def data(self):
        ret = super().data
        return ReturnDict(ret, serializer=self)

ReturnDict inherits from OrderedDict , but you still get a new dictionary every time you access Serializer.data . ReturnDict继承自OrderedDict ,但每次访问Serializer.data时仍然会得到一个字典。

The real data is in _data , however as noted by the underscore you might not want to modify that either as it is not intended to be public.真实数据在_data ,但是正如下划线所指出的,您可能不想修改它,因为它不打算公开。 The values are filled by Serializer.to_representation() which you could override on the viewset.这些值由Serializer.to_representation()填充,您可以在视图集上覆盖它。

As for the second part: ModelViewSet defines get_serializer() that is called with the request POST data to create the serializer you want to modify.至于第二部分: ModelViewSet定义get_serializer()被调用请求POST数据来创建你要修改的串行器。 I'd suggest try to change the input data before the serializer is created, instead.我建议尝试在创建序列化程序之前更改输入数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM