How to PATCH a single field using Django Rest Framework?

后端 未结 5 1859
逝去的感伤
逝去的感伤 2020-12-23 12:31

I have a model \'MyModel\' with many fields and I would like to update a field \'status\' using PATCH method. I\'m using class based views. Is there any way to implement PAT

5条回答
  •  南方客
    南方客 (楼主)
    2020-12-23 13:09

    If anyone is still planning to find a simple solution using ModelSerializer without changing much of your views, you can subclass the ModelSerializer and have all your ModelSerializers inherit from that.

    class PatchModelSerializer(serializers.ModelSerializer):
        def __init__(self, *args, **kwargs):
            kwargs['partial'] = True
            super(PatchModelSerializer, self).__init__(*args, **kwargs)
    
    class ArticleSerializer(PatchModelSerializer):
        class Meta:
            model = Article
    

提交回复
热议问题