I'd like to use a field from a parent class as a constraint condition in a child class.
models.py
class ParentClass(object):
...
is_public = models.BooleanField(default=False)
class ChildClass(ParentClass):
...
price = models.DecimalField(max_digits=6, decimal_places=2, null=True)
class Meta:
constraints = [
models.CheckConstraint(
check=Q(price__isnull=True) & Q(is_public=True), # <- here
name='price_exists_check',
)
]
When I attempt to migrate, I see this error in my terminal:
myapp.ChildClass: (models.E016) 'constraints' refers to field 'is_public'
which is not local to model 'ChildClass'.
HINT: This issue may be caused by multi-table inheritance.
It's obvious why I am seeing this error (is_public
field lives in ParentClass
). My question is, is it simply not possible then, or can I refactor something?
What is my end-goal?
To not let an instance of ChildClass
is_pulic
change to True
if the price
is null
. I'd like to enforce this at the database level.
Is there a way and if so, what needs to change?