Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#26502 closed Uncategorized (invalid)

Lookup of attribute 'id' on model inheritance models in InlineModelAdmin fails when using fk_name of parent model — at Version 11

Reported by: Waldemar Hamm Owned by: nobody
Component: Forms Version: 1.8
Severity: Normal Keywords: id, model inheritance, inherited model, inline
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Waldemar Hamm)

Having two models, of which one inherits from another, for example:

class Person(models.Model):
    firstname = models.CharField(max_length=255, blank=True)
    lastname = models.CharField(max_length=255)
    person_something = models.ForeignKey(Something)

class Author(Person):
    publisher = models.CharField(max_length=255)
    author_something = models.ForeignKey(Something)

I can't access the id field like so:

class AuthorInline(admin.StackedInline):
    model = Author
    fk_name = 'person_something'
    fields = ['id', 'lastname', 'publisher']
    readonly_fields = fields

class SomethingAdmin(admin.ModelAdmin):
    inlines = [ AuthorInline ]
    list_display = ('somethinga', 'somethingb')

I get a KeyError and it says:
"Key 'id' not found in 'AuthorForm'"

Using Author._meta.get_all_field_names() the field id is definitely in the model, though. Removing 'id' from the fields list makes everything work.

Am I doing something wrong or is this a legit bug?

Change History (11)

comment:1 by Waldemar Hamm, 8 years ago

Description: modified (diff)
Summary: Lookup of attribute 'id' on model inheritance models in InlineModelAdmin failsLookup of attribute 'id' on model inheritance models in InlineModelAdmin fails when using fk_name of parent model

comment:2 by Waldemar Hamm, 8 years ago

Description: modified (diff)

comment:3 by Tim Graham, 8 years ago

I think you need to use person_ptr in the fields list rather than id. I created a PR to add the list of possible fields to the KeyError message which would hopefully have helped you debug this. In this case, it adds "Choices are: DELETE, lastname, person_ptr, person_something, publisher."

comment:4 by Tim Graham <timograham@…>, 8 years ago

In 3cb63b0:

Refs #26502 -- Added choices to Form.getitem() KeyError message.

comment:5 by Tim Graham, 8 years ago

Component: contrib.adminForms
Resolution: invalid
Status: newclosed

comment:6 by Waldemar Hamm, 8 years ago

Thanks a lot! :)

comment:7 by Waldemar Hamm, 8 years ago

Resolution: invalid
Status: closednew

I tried using your suggestion but changing 'id' to 'person_ptr' in fields doesn't make the actual field appear within the admin object view.

It just resolved the error so the view appears at all. Why isn't the id/person_ptr field displayed?

Last edited 8 years ago by Waldemar Hamm (previous) (diff)

comment:8 by Tim Graham, 8 years ago

Resolution: invalid
Status: newclosed

It's not an editable field as described in #11097. We might add some clarifying documentation as part of that ticket.

comment:9 by Waldemar Hamm, 8 years ago

It's true that it is not visible in the example code I'm providing but actually I am using all of the provided fields as read_only fields. It's still not visible.

Is this also intended?

comment:10 by Tim Graham, 8 years ago

Not sure, you should provide your actual code. That said, "is it a bug?" questions are better handled through our support channels.

comment:11 by Waldemar Hamm, 8 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top