【发布时间】:2020-05-13 18:16:35
【问题描述】:
有两个模型,父母和孩子,我想设置孩子继承的属性之一的值。
例如,在以下代码中,颜色属性将在创建RedCat 对象时设置。
# Parent class
class Cat(models.Model):
name = models.CharField(max_length=10)
color = models.CharField(max_length=10)
class Meta:
abstract = True
# Child class
class RedCat(Cat):
color = 'red' # <-- Doesn't work!
我正在考虑覆盖父属性或仅在子属性上使用该属性,但我想知道,是否有正确/更好的方法在 Django 模型中为继承属性设置默认值?
【问题讨论】:
标签: python django python-3.x django-models django-3.0