4

How to perform object-level conditional validation using django-rest-framework. If we use validate(self, attrs) in serializer, validation happens for all calls. I should know and apply it to certain http verbs. ex: I want to perform a object-level validation only for PUT.

1 Answer 1

6

You can get the request inside a serializer method using:

request = self.context['request']

You can then do any conditional validation using 'request.method'

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer. In general, do you suggest Serializer class is the right place to write business validations?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.