Hi,
I'm trying to create a custom user model where I inherit from AbstractUser with constraints on fields which should depend on some conditions. One such field is a CharField that has a length restriction:
phonenumber = models.CharField(max_length=20)
So I added a CheckConstraint like this:
models.CheckConstraint(
condition=models.Q(is_superuser__exact=True) | models.Q(phonenumber__length__gte=7),
name="%(app_label)s_%(class)s_phonenumber_length",
),
The idea here is that the superuser should be exempt from this condition. But when I try to apply the migration, I get an IntegrityError because the superuser's phone number is empty. In theory, I would like to make the field more flexible by allowing some special characters and spaces between the numbers, eg. +1 (234) 456 789 or something, then normalize it before storage.
How can I solve this problem, please?