1

I use Django 1.10. I have the following model structure:

class GenericPage(models.Model):
    """Abstract page, other pages inherit from it."""
    book = models.ForeignKey('Book', on_delete=models.CASCADE)

    class Meta:
        abstract = True

class GenericColorPage(models.Model):
    """Abstract page that is sketchable and colorable, other pages inherit from it."""
    sketched = models.BooleanField(default=False)
    colored = models.BooleanField(default=False)

    class Meta:
        abstract = True

class GenericBookPage(GenericColorPage):
    """A normal book page, with a number. Needs to be storyboarded and edited."""

    ###
    #various additional fields
    ###

    class Meta:
        # unique_together = (('page_number', 'book'),) # impedes movement of pages
        ordering = ('-book', '-page_number',)
        abstract = True

    objects = BookPageManager()  # the manager for book pages

class BookPage(GenericBookPage):
    """Just a regular book page with text (that needs to be proofread)"""
    proofread = models.BooleanField(default=False)

Additionally, an excerpt from Admin:

class BookPageAdmin(admin.ModelAdmin):
    # fields NOT to show in Edit Page.
    list_display = ('__str__', 'page_name', 'sketched', 'colored', 'edited', 'proofread',)
    list_filter = ('book',)
    readonly_fields = ('page_number',)  # valid page number is assigned via overridden save() in model
    actions = ['delete_selected',]

I tried to do ./manage.py makemigrations but if throws the following errors:

<class 'progress.admin.BookPageAdmin'>: (admin.E116) The value of 'list_filter[0]' refers to 'book', which does not refer to a Field.
progress.BookPage: (models.E015) 'ordering' refers to the non-existent field 'book'.

In the past, when I did not use the abstracts and just put everything into BookPage model, it all worked fine. But it seems that Meta and Admin don't see the fields in parent classes. Am I missing something? Is there a way to make them read fields from abstract parents?

1 Answer 1

2

In the past, when I did not use the abstracts and just put everything into BookPage model, it all worked fine

Of course it worked fine because you put everything inside BookPage which is not an abstract class which means that table (and, thus, fields) will be created.

But it seems that Meta and Admin don't see the fields in parent classes. Am I missing something?

You're missing the fact that none of your models inherits from the GenericPage abstract model. Thus, the book field is never created.

Is there a way to make them read fields from abstract parents?

You must create/modify a model that inherits from an abstract model. Maybe, do this:

class GenericBookPage(GenericColorPage, GenericPage):

which allows you to inherit both GenericColorPage and GenericPage fields. When I say inherit I mean when the migrate command runs to actually create the database table and the relevant columns (model fields).

1
  • Oh, boy, I've made a mistake there. It should be GenericColorPage(GenericPage): instead! I almost thought that Django was unable to traverse the tree of abstracts, but there was no tree in the first place. Thanks for pointing it out!
    – Highstaker
    Commented Apr 20, 2017 at 6:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.