Issue
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.
Solution
You need to override the field specifically and add your own validator. You can read here for more detail http://www.django-rest-framework.org/api-guide/serializers/#specifying-fields-explicitly. 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
Answered By - Edwin Lunando
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.