different validation in drf serializer per request

2019-04-08 02:34发布

问题:

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': []
                 }

回答1:

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