Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

different validation in drf serializer per request method

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': []
                 }
like image 688
Mr T. Avatar asked Jul 03 '16 14:07

Mr T.


1 Answers

I need more space than comments provide to make my meaning clear. So here is what I suggest:

  1. Different formatting means different serializers.

    So here you have, for instance a MyModelSerializer and a MyModelCreationSerializer. Either create them independently, or have one inherit the other and specialize it (if it makes sense).

  2. Use the appropriate GenericAPIView hook to return the correct serializer class depending on self.action. A very basic example could be:

    class MyModelDetailAPI(GenericAPIView):
        # serializer_class = unneeded as we override the hook below
    
        def get_serializer_class(self):
            if self.action == 'create':
                return MyModelCreationSerializer 
            return MyModelSerializer
    

    Default actions in regular viewsets are documented here, they are:

    • create: POST method on base route url
    • list: GET method on base route url
    • retrieve: GET method on object url
    • update: PUT method on object url
    • partial_update: PATCH method on object url
    • destroy: DELETE method on object url
like image 185
spectras Avatar answered Sep 23 '22 02:09

spectras