Python 在 Django Rest Framework 中更新/附加 serializer.data
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37111118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Update/append serializer.data in Django Rest Framework
提问by user4812479812
How can I update/append serializer.data
in Django Rest Framework?
如何serializer.data
在 Django Rest Framework 中更新/追加?
data = serializer.data.update({"item": "test"}) not working
data = serializer.data.update({"item": "test"}) 不工作
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.data, status=status.HTTP_201_CREATED)
serializer.data
is <class 'rest_framework.utils.serializer_helpers.ReturnDict'>
serializer.data
是 <class 'rest_framework.utils.serializer_helpers.ReturnDict'>
回答by Mohammed Shareef C
Unfortunately, serializer.data
is a property of the class and therefore immutable. Instead of adding items to serializer.data
you can copy serializer.data
to another dict
. You can try this:
不幸的是,serializer.data
是类的属性,因此是不可变的。而不是增加项目的serializer.data
可复制serializer.data
到另一个dict
。你可以试试这个:
newdict={'item':"test"}
newdict.update(serializer.data)
return Response(newdict, status=status.HTTP_201_CREATED)
回答by Linovia
You don't.
你没有。
If you need to pass extra data to the serializer's create/update please do so while calling serializer.save() as explained in the documentation
如果您需要将额外数据传递给序列化程序的创建/更新,请在调用 serializer.save() 时执行此操作,如文档中所述
回答by lgespee
Alternatively you might use SerializerMethodField to add additional data by adding a custom method to the serializer.
或者,您可以使用 SerializerMethodField 通过向序列化程序添加自定义方法来添加其他数据。
http://www.django-rest-framework.org/api-guide/fields/#serializermethodfield
http://www.django-rest-framework.org/api-guide/fields/#serializermethodfield
You can use such a method to return any data, wether in context of the model or not.
您可以使用这样的方法返回任何数据,无论是否在模型上下文中。
class UserSerializer(serializers.ModelSerializer):
days_since_joined = serializers.SerializerMethodField()
class Meta:
model = User
def get_days_since_joined(self, obj):
return (now() - obj.date_joined).days
回答by anjaneyulubatta505
We can update the data passed in response with serializer._data
我们可以更新响应中传递的数据 serializer._data
sample code
示例代码
class SampleAPIView(generics.CreateAPIView)
serializer_class = SampleSerializer
def perform_update(self, serializer):
application = serializer.save(status='pending')
my_response_data = {'id': 110, 'name': 'Batta'}
serializer._data = out_data
serializer._data
will make the magic.
Reference: https://github.com/encode/django-rest-framework/blob/master/rest_framework/serializers.py#L260
serializer._data
会变魔术。参考:https: //github.com/encode/django-rest-framework/blob/master/rest_framework/serializers.py#L260
回答by Andre Machado
The serializer.data
object is a instance of ReturnList that is immutable. What you can do to workaround this limitation is transform the serializer.data
object into a simple Python list(), then append the value you want so you can use your transformed list in Response() method like this:
该serializer.data
对象是不可变的 ReturnList 的实例。你可以做些什么来解决这个限制是将serializer.data
对象转换为一个简单的 Python list(),然后附加你想要的值,这样你就可以在 Response() 方法中使用转换后的列表,如下所示:
def get(self, request):
serializer = YourAmazingSerializer(many=True)
new_serializer_data = list(serializer.data)
new_serializer_data.append({'dict_key': 'dict_value'})
return Response(new_serializer_data)
Then, your response will have your new obj
然后,您的回复将包含您的新对象
回答by Rohit Arya
Apart from what @Linoviahas suggested, you can also do something like this:
除了@Linovia 的建议之外,您还可以执行以下操作:
serializer.data['item'] = 'test'
Make sure to call this before serializer.is_valid
is called.
确保在调用之前serializer.is_valid
调用它。
If you want to add data after serializer
validation, you should do like this:
如果你想在serializer
验证后添加数据,你应该这样做:
serializer.validated_data['item'] = 'test'