[英]patch in serializer django rest
I have 1 parent and 1 child serializers, right now child serializer inherits all fields, functions and methods from the parent serializer.我有 1 个父序列化器和 1 个子序列化器,现在子序列化器继承父序列化器的所有字段、函数和方法。 I would like to modify patch request in child serializer, that while request is patch, then some fields will be unavailable for updating, cause there will be completely different two urls.
我想在子序列化程序中修改补丁请求,当请求是补丁时,某些字段将无法更新,因为两个url会完全不同。 For example in child class there will be unable to update name and surname.
例如在子类中将无法更新姓名和姓氏。
class Parent(serializers.ModelSerializer):
class Meta(BaseMeta):
model = Account
fields = BaseMeta.fields + (
'name', 'surname', 'age', 'city', 'country', 'job', 'family')
extra_kwargs = {'name': {'required': True, 'allow_blank': False, 'allow_null': False, 'trim_whitespace': False},
'surname': {'required': True, 'allow_blank': False, 'allow_null': False, 'trim_whitespace': False},
'country': {'read_only': True},
'job': {'required': True, 'allow_blank': False, 'allow_null': False, },
}
class Child(ParentSerializer):
class Meta(BaseMeta):
model = Account
fields = BaseMeta.fields + ()
extra_kwargs = {
}
In order to implement PATCH
api, you can create a detail view.为了实现
PATCH
api,您可以创建一个详细视图。
from rest_framework import generics, mixins
from .models import Child
from .serializers import Child as ChildSerializer
class ChildDetail(mixins.UpdateModelMixin, generics.GenericAPIView):
queryset = Child.objects.all()
serializer_class = ChildSerializer
def patch(self, request, *args, **kwargs):
return self.partial_update(request, *args, **kwargs)
In the detail view, you can also add get
, retrieve
, put
, delete
method.在详细视图中,您还可以添加
get
、 retrieve
、 put
、 delete
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.