Let's say I have a model like this:
class MyModel(models.Model): first_field = models.CharField() second_field = models.CharField()
and an API view like this:
class MyModelDetailAPI(GenericAPIView): serializer_class = MyModelSerializer def patch(self, request, *args, **kwargs):
first_field is a field that is only inserted into the POST method (and is required), but the user cannot change its value with each update, so the field in the PATCH method is optional.
How can I write my serializer so that first_field required in POST, but not required for PATCH. Is there a way to dynamically set the required field, so I can still use the DRF verification mechanism? What type of validator dispatcher for each request?
I want something like this, for example:
class MyModelSerializer(serializers.ModelSerializer): class Meta: model = MyModel fields = { 'POST': ['first_field'] 'PATCH': [] }
Mr T. source share