Lets say i have a model like so:
class MyModel(models.Model):
first_field = models.CharField()
second_field = models.CharField()
and an API view like so:
class MyModelDetailAPI(GenericAPIView):
serializer_class = MyModelSerializer
def patch(self, request, *args, **kwargs):
# Do the update
def post(self, request, *args, **kwargs):
# Do the post
The first_field
is a field that is only inserted in the POST method (and is mandatory) but on each update, the user can't change its value so the field in the PATCH method is not mandatory.
How can i write my serializer so that the first_field
is required on POST but not required on PATCH. Is there any way of dynamically setting the required field so i can still use the DRF validation mechanism? Some sort of validator dispatcher per request method?
I want something like this for example:
class MyModelSerializer(serializers.ModelSerializer):
class Meta:
model = MyModel
fields = {
'POST': ['first_field']
'PATCH': []
}
I need more space than comments provide to make my meaning clear. So here is what I suggest:
Different formatting means different serializers.
So here you have, for instance a
MyModelSerializer
and aMyModelCreationSerializer
. Either create them independently, or have one inherit the other and specialize it (if it makes sense).Use the appropriate
GenericAPIView
hook to return the correct serializer class depending onself.action
. A very basic example could be:Default actions in regular viewsets are documented here, they are:
create
: POST method on base route urllist
: GET method on base route urlretrieve
: GET method on object urlupdate
: PUT method on object urlpartial_update
: PATCH method on object urldestroy
: DELETE method on object url