I am having trouble with my ModelSerializer
class Recall(models.Model):
thermal_step = models.BooleanField()
have_inventory = models.BooleanField()
have_adverse = models.BooleanField()
class OrderRecallSerializer(serializers.ModelSerializer):
class Meta:
model = Recall
fields = ['thermal_step', 'have_inventory', 'have_adverse']
s = OrderRecallSerializer(data={})
s.is_valid()
>>> True
s.save()
>>> django.db.utils.IntegrityError: null value in column
"thermal_step" violates not-null constraint
In django BooleanField is always set to blank=True due to browsers don't send unchecked checkbox in POST data.
ModelForm sets model field to False in this case. When using DRF ModelSerializer, generated BooleanField is created with required=False, and when the field is not present in data, passed to serializer, model field is set to None and raises IntegrityError when saving model.
Should I explicitly specify BooleanField(default=False) in my ModelSerializer, or I am missing something? I would expect ModelSerializer to behave similar to ModelForm - set value to False when it is not present, which is equal to generating BooleanField with default=False automatically.
I think getting IntegrityError for default behaviour is not what expected, maybe I should submit a bug?
models.NullBooleanField()
ormodels.BooleanField(required=False, null=True, default=False)
– alfonso.kim