Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

127
Visualizações
Admin and Meta don't see the fields of parent abstract classes

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?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda