可以覆盖Django中抽象模型的默认值吗?

时间:2018-12-20 16:42:42

标签: django

说我有一个看起来像这样的抽象模型:

class CommonInfo(models.Model):
    id = models.BigIntegerField(primary_key=True, default=get_id, editable=False)

    class Meta:
        abstract = True

还有一个继承这样的抽象模型的类:

class Concrete(CommonInfo):
    name = models.CharField()

然后可以在以后创建一个Concrete实例,该实例覆盖id中的CommonInfo。例如:

Concrete.objects.create(id=1, name="test")

我已经尝试过这种方法,并且似乎可行,但是当我尝试对这些对象进行排序时,我得到一个TypeError: '<' not supported between instances of 'Concrete' and 'Concrete',如以下问题所示:Django: TypeError: '<' not supported between instances (model objects)

但是,当我不重写id并仅使用nameConcrete.objects.create(name="test")创建对象时,不会引发此类错误。这使我相信实际上无法做我想做的事情,是对的还是其他原因导致TypeError

0 个答案:

没有答案
相关问题