from rest_framework import serializers
class CommentSerializer(serializers.Serializer):
email = serializers.EmailField()
content = serializers.CharField(max_length=200)
created = serializers.DateTimeField()
def restore_object(self, attrs, instance=None):
"""
Given a dictionary of deserialized field values, either update
an existing model instance, or create a new model instance.
"""
if instance is not None:
instance.email = attrs.get('email', instance.email)
instance.content = attrs.get('content', instance.content)
instance.created = attrs.get('created', instance.created)
return instance
return Comment(**attrs)
比如,这其中的attrs是?
迷茫2017-04-17 13:45:30
This is the parameter list of python. The two asterisks are variable parameters. The
parameter received by restore_object
attrs
is of dict type. When passed to the Comment
function, add two asterisks in front of it to convert it into a variable parameter list.
For example
attrs = {'a':1, 'b':2}
The actual call of the Comment function will become:
Comment(a=1, b=2)
Reference: http://n3xtchen.github.io/n3xtchen/python/2014/08/08/python---args-and-kwargs/
阿神2017-04-17 13:45:30
attr is the parameter of the function. What it is depends on your own definition