3

Django 文档中指出,为了使用多重继承,要么必须

在基本模型中使用显式 AutoField

或者

使用共同的祖先来保存 AutoField

就我而言,我确实有一个共同的祖先,如以下设置(取自文档):

class Piece(models.Model):
    piece_id = models.AutoField(primary_key=True)

class Article(Piece):
    pass

class Book(Piece):
    pass

class BookReview(Book, Article):
    pass

不幸的是,这会导致以下错误:

$ python manage.py check
SystemCheckError: System check identified some issues:

ERRORS:
testapp.BookReview: (models.E005) The field 'piece_ptr' from parent model 'testapp.book' clashes with the field 'piece_ptr' from parent model 'testapp.article'.

System check identified 1 issue (0 silenced).

有什么办法吗?


编辑:Django 版本是 1.8.2

4

1 回答 1

2

我刚刚发现我实际上可以将链接命名为父链接:

class Piece(models.Model):
    pass

class Article(Piece):
    article_to_piece = models.OneToOneField(Piece, parent_link=True)

class Book(Piece):
    book_to_piece = models.OneToOneField(Piece, parent_link=True)

class BookReview(Book, Article):
    pass

不过,我仍然对其他解决方案感到好奇!

于 2015-06-29T15:00:28.250 回答