我想使用父类中的字段作为子类中的约束条件。
模型.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',
)
]
当我尝试迁移时,我在终端中看到此错误:
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.
很明显,为什么我会看到此错误(is_public
字段位于ParentClass
)。我的问题是,那是不可能的,还是我可以重构一些东西?
我的最终目标是什么?
不让实例ChildClass
is_pulic
更改为True
if the price
is null
。我想在数据库级别强制执行此操作。
有没有办法,如果有,需要改变什么?