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是?
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 theComment
function, add two asterisks in front of it to convert it into a variable parameter list.For example
The actual call of the Comment function will become:
Reference: http://n3xtchen.github.io/n3xtchen/python/2014/08/08/python---args-and-kwargs/
attr is the parameter of the function. What it is depends on your own definition