Search code snippets, questions, articles...

Django Rest Framework field level serializer validation.

Your validate_field_name methods should return the validated value or raise a serializers.ValidationError. For example:
from rest_framework import serializers

class BlogPostSerializer(serializers.Serializer):
    title = serializers.CharField(max_length=100)
    content = serializers.CharField()

    def validate_title(self, value):
        """
        Check that the blog post is about Django.
        """
        if 'django' not in value.lower():
            raise serializers.ValidationError("Blog post is not about Django")
        return value

Search Index Data (The code snippet can also be found with below search text)

serializer
Was this helpful?
0 Comments
Programming Feeds
Learn something new everyday on Devsheet