Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django rest framework - Field level validation in serializer

I have a serializer and I'm trying to add field level validation and I need to verify if some charfields of the serialize are empty or not, and if a boolean field is true or false.

I have this serializer but I never return an error even if Ficha_publicada is false

class PublicarSerializer(serializers.Serializer):

    Titulo = serializers.CharField(required=True)
    Ficha_publicada = serializers.BooleanField()

    def validate_Titulo(self, attrs, source):
        value = attrs[source]

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

    class Meta:
        model = Fichas
like image 952
Audoen Avatar asked Dec 01 '25 21:12

Audoen


1 Answers

And for Django rest framework 3.0 and more recent versions:

def validate_Titulo(self, value):
like image 190
Vladir Parrado Cruz Avatar answered Dec 07 '25 15:12

Vladir Parrado Cruz