如何使用unique_together获得抽象模型?

时间:2011-08-29 22:47:10

标签: django django-models

为什么我要来:

Error: One or more models did not validate:
test.test: "unique_together" refers to slug. This is not
in the same model as the unique_together statement.
test.test: "unique_together" refers to version. This is not
in the same model as the unique_together statement.

我有这样的模型定义:

class SlugVersion(models.Model):
    class Meta:
        abstract = True
        unique_together = (('slug', 'version'),)

    slug = models.CharField(db_index=True, max_length=10, editable=False)
    version = models.IntegerField(db_index=True, editable=False)

class Base(SlugVersion):
    name = models.CharField(max_length=10)

class Test(Base):
    test = models.IntegerField()

我有Django 1.3。

2 个答案:

答案 0 :(得分:2)

Django似乎是bug

答案 1 :(得分:0)

这是因为Test使用multi-table inheritanceBase继承,它为这两个模型创建了单独的表,并将它们与OneToOne关系链接起来。因此,slugversion字段仅存在于Base表中,而不存在于测试表中。

您是否需要Base非抽象?换句话说,您会创建任何实际的Base个对象吗?如果将其设置为抽象,则此错误消失。