1

我想知道 - 是否可以继承这样的类?

例如,我有 2 个抽象类:

    class Book(models.Model):
        name = models.TextField()

        class Meta:
            abstract = True

    class Page(models.Model)
        num = models.IntegerField()
        book = models.ForeignKey('Book')

        class Meta:
            abstract = True

所以 - 我想为这两个创建继承类,让它们成为BigBookBigBookPage

但是如果我这样做,python 对我说,我的 FK 字段不能与抽象模型有关系。而且我找不到在继承模型中重新定义 FK 的方法。那么我是否必须只在继承模型中创建外键 - 而不是父母?

如果我有相同的模型方法,使用在父模型中定义的外键......我必须将它们移动到每个孩子 - 这样他们就可以使用他们的外键?

4

1 回答 1

1

It sounds like you want to mark Book as proxy=True. See proxy models. It will create a book model, but also let you have other models that inherit from it letting you customize the functionality of the subclasses.

class Book(models.Model):
    class Meta:
        proxy = True

class BigBook(Book):
    # BigBook properties go here.
于 2013-08-13T12:11:51.563 回答