Cristian Rojas Cristian Rojas - 4 months ago 40
AngularJS Question

django-rest-framework how to make model serializer fields required

I have a model that I'm filling out step by step, it means I'm making a form wizard.

Because of that most fields in this model are required but have

null=True, blank=True
to avoid raising not null errors when submitting part of the data.

I'm working with Angular.js and django-rest-framework and what I need is to tell the api that x and y fields should be required and it needs to return a validation error if they're empty.


You need to override the field specifically and add your own validator. You can read here for more detail This is the example code.

def required(value):
    if value is None:
        raise serializers.ValidationError('This field is required')

class GameRecord(serializers.ModelSerializer):
    score = IntegerField(validators=[required])

    class Meta:
        model = Game