Skip to content

Instantly share code, notes, and snippets.

@aballah-chamakh
Created December 10, 2019 05:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aballah-chamakh/6b5cb34ea74f224fed121629025b82c4 to your computer and use it in GitHub Desktop.
Save aballah-chamakh/6b5cb34ea74f224fed121629025b82c4 to your computer and use it in GitHub Desktop.
from rest-framework import serializers
from .models import Review
class ReviewSerializer(serializers.Serializer):
class Meta :
model = Review
fields = ['username','content']
# to validate any field override the function validate_<field>
def validate_username(self,value):
if len(value) > 20 :
raise serializers.ValidationError("the length of your username should be less than 20 character")
return value
# to validate on the serializer level
def validate(self, data):
if data['username'] == data['content'] :
raise serializers.ValidationError("the content value should not be the same as the username value ")
return value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment