Home  >  Q&A  >  body text

python中的attrs是什么?

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是?

大家讲道理大家讲道理2741 days ago1422

reply all(2)I'll reply

  • 迷茫

    迷茫2017-04-17 13:45:30

    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/

    reply
    0
  • 阿神

    阿神2017-04-17 13:45:30

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

    reply
    0
  • Cancelreply