I have 1 parent and 1 child serializers, right now child serializer inherits all fields, functions and methods from the parent serializer. 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. 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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.