开发者

How to have abstract model with unique_together?

Why I am getting:

Error: One or more models did not validate:
test.test: "unique_together" refers to slug. This is not
in the same开发者_如何学C 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.

I have such model definition:

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()

I have Django 1.3.


It seems it is a bug in Django.


It's because Test is inheriting from Base using multi-table inheritance, which creates separate tables for the two models and links them with a OneToOne relationship. So the slug and version fields exist only in the Base table but not in the Test table.

Do you need Base to be non-abstract? In other words, will you create any actual Base objects? If you set it to be abstract, this error goes away.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜