python中的attrs是什么?
大家讲道理
大家讲道理 2017-04-17 13:43:30
0
2
1456
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是?

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(2)
迷茫

This is the parameter list of python. The two asterisks are variable parameters. The
parameter received by restore_objectattrs 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/

阿神

attr is the parameter of the function. What it is depends on your own definition

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template