[Django]-Django rest framework – Field level validation in serializer

3👍

use attrs['Ficha_publicada'] :

if not attrs['Ficha_publicada']:
    raise serializers.ValidationError("Ficha no publicada")
return attrs

5👍

And for Django rest framework 3.0 and more recent versions:

def validate_Titulo(self, value):

3👍

For Django 1.8 you need to use a slightly different method signature.

From (<1.8)

def validate_Titulo(self, attrs, source):

To (1.8)

def validate_Titulo(self, attrs, source=None):

If you do not add the default None to the source argument in Django 1.8 you will get a TypeError exception saying:

validate_Titulo() missing 1 required positional argument: ‘source’

Leave a comment