简体   繁体   中英

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

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM