10

找到 Django-REST 框架文档,尽管它很长,但对我来说背景太浅了。

restore_object 方法的 attrs 函数有什么作用?

instance.title = attrs.get('title', instance.title)

第二个参数代表什么,我将如何在文档中寻找这在未来意味着什么?

也不确定双星号的return Snippet(**attrs)含义。这与 **keywArgs 不同?哪些参数被传递回反序列化的 Snippet 对象?

在文档的另一部分,我在 restore_object()instance.title = attrs['title']中看到了我希望人们能够看到我的困惑。

谢谢你

class SnippetSerializer(serializers.Serializer):
    pk = serializers.Field()  # Note: `Field` is an untyped read-only field.
    title = serializers.CharField(required=False,
                                  max_length=100)
    code = serializers.CharField(widget=widgets.Textarea,
                                 max_length=100000)
    linenos = serializers.BooleanField(required=False)
    language = serializers.ChoiceField(choices=LANGUAGE_CHOICES,
                                       default='python')
    style = serializers.ChoiceField(choices=STYLE_CHOICES,
                                    default='friendly')

    def restore_object(self, attrs, instance=None):
        """
        Create or update a new snippet instance.
        """
        if instance:
            # Update existing instance
            instance.title = attrs.get('title', instance.title)
            instance.code = attrs.get('code', instance.code)
            instance.linenos = attrs.get('linenos', instance.linenos)
            instance.language = attrs.get('language', instance.language)
            instance.style = attrs.get('style', instance.style)
            return instance

        # Create new instance
        return Snippet(**attrs)
4

2 回答 2

6

我已经稍微更新了文档,以使其更加清晰......

http://django-rest-framework.org/tutorial/1-serialization.html#creating-a-serializer-class

该方法现在读取...

def restore_object(self, attrs, instance=None):
    """
    Create or update a new snippet instance, given a dictionary
    of deserialized field values.

    Note that if we don't define this method, then deserializing
    data will simply return a dictionary of items.
    """
    if instance:
        # Update existing instance
        instance.title = attrs.get('title', instance.title)
        instance.code = attrs.get('code', instance.code)
        instance.linenos = attrs.get('linenos', instance.linenos)
        instance.language = attrs.get('language', instance.language)
        instance.style = attrs.get('style', instance.style)
        return instance

    # Create new instance
    return Snippet(**attrs)

**attrs样式使用 Python 的标准关键字扩展。请参阅此处以获得很好的解释。

它最终会等同于Snippet(title=attrs['title'], code=attrs['code'], ...)

希望有帮助!

于 2013-03-17T20:12:09.860 回答
4

在 DRF 3.0+ 中删除了 restore_object 方法。现在您可以使用两种不同的方法:.create 和 .update

def update(self, instance, validated_data): 
    instance.title = validated_data.get('title', instance.title) 
    instance.code = validated_data.get('code', instance.code) 
    instance.linenos = validated_data.get('linenos', instance.linenos)
    instance.language = validated_data.get('language', instance.language)         
    instance.style = validated_data.get('style', instance.style)  
    instance.save() 
    return instance 

def create(self, validated_data): 
    return Snippet.objects.create(**validated_data) 

发行说明: http: //www.django-rest-framework.org/topics/3.0-announcement/

于 2015-03-30T19:53:07.587 回答