Question

Hello this is my first question here. I am beginner and i am making my first API in DRF. I am familiar with the MVT(MDC) model of django. My question is very simple but their is no good documentation for DRF to understand many things. What is the purpose of the get_validation_exclusions function in the this Serializer ?Do you know where i can find a very detailed documentation for DRF ?

class PostSerializer(serializers.ModelSerializer):
   author = UserSerializer(required=False)
   photos = serializers.HyperlinkedIdentityField('photos', view_name='postphoto-list')
   # author = serializers.HyperlinkedRelatedField(view_name='user-detail',       lookup_field='username')

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

class Meta:
    model = Post

Source: http://blog.kevinastone.com/getting-started-with-django-rest-framework-and-angularjs.html#model-serializers

Was it helpful?

Solution

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.

OTHER TIPS

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']
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top