[Answered ]-Django: constraint with nullable multiple field conditions

1👍

This is not due to the constraints, you should specify blank=True [Django-doc] when you want to allow to leave form fields blank, and thus default to None/NULL:

class SomeModel(models.Model):
    # …
    a = models.IntegerField(null=True, blank=True)
    b = models.DateTimeField(null=True, blank=True)
    c = models.DateTimeField(null=True, blank=True)

    class Meta:
        constraints = [
            models.CheckConstraint(
                check=Q(a=None, b=None, c=None) |
                      Q(a__isnull=False, b__isnull=False, c__isnull=False),
                name='all_or_none_null'
            )
        ]

Leave a comment