Constantinius
Constantinius

Reputation: 35089

Django multiple inheritance E005

In the Django docs it is stated that in order to use multiple inheritance one either has to

use an explicit AutoField in the base models

or

use a common ancestor to hold the AutoField

In my case I do have a common ancestor like in the following setup (as taken from the docs):

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

class Article(Piece):
    pass

class Book(Piece):
    pass

class BookReview(Book, Article):
    pass

Unfortunately this results in the following error:

$ 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).

Any way around this?


EDIT: Django version is 1.8.2

Upvotes: 3

Views: 724

Answers (1)

Constantinius
Constantinius

Reputation: 35089

I just found out that I can actually name the link to the parent:

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

I'm still curious for other solutions though!

Upvotes: 2

Related Questions