[Django]-Django Restful Framework model serializers get_validation_exclusions

2👍

The Django REST Framwork website is going to be the best actual documentation for DRF. But it doesn’t cover many of the low-level methods, such as this one. One of the thing I love most about using Python is that most packages have to include the source. I have always found the source to be the best documentation and best way to learn from more experienced developers. Using a tool like Sourcegraph might make reading through that source easier.

As for this specific method, it’s providing a list of fields not to run validation on. By default it returns a list of fields marked read-only on the serializer. This list then gets past as the exclude kwarg to the models full_clean method.

👤jsatt

1👍

i fixed it by updating the get_validation_exclusions signature like the following. DRF updated the signature in the latest version.

def get_validation_exclusions(self, instance=None):
    # Need to exclude `user` since we'll add that later based off the request
    exclusions = super(PostSerializer, self).get_validation_exclusions(instance)
    return exclusions + ['author']

Leave a comment