[Django] #26502: Lookup of attribute 'id' on model inheritance models in InlineModelAdmin fails

52 views
Skip to first unread message

Django

unread,
Apr 14, 2016, 7:34:31 AM4/14/16
to django-...@googlegroups.com
#26502: Lookup of attribute 'id' on model inheritance models in InlineModelAdmin
fails
-------------------------+-------------------------------------------------
Reporter: | Owner: nobody
Braintelligence |
Type: | Status: new
Uncategorized |
Component: | Version: 1.8
contrib.admin | Keywords: id, model inheritance, inherited
Severity: Normal | model, inline
Triage Stage: | Has patch: 0
Unreviewed |
Easy pickings: 0 | UI/UX: 0
-------------------------+-------------------------------------------------
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)

class Author(Person):
publisher = models.CharField(max_length=255)
}}}

I can't access the id field like so:

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

class SomethingAdmin(admin.ModelAdmin):
resource_class = Something
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?

--
Ticket URL: <https://code.djangoproject.com/ticket/26502>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Apr 14, 2016, 7:40:43 AM4/14/16
to django-...@googlegroups.com
#26502: Lookup of attribute 'id' on model inheritance models in InlineModelAdmin
fails when using fk_name of parent model
-------------------------------------+-------------------------------------
Reporter: Braintelligence | Owner: nobody
Type: Uncategorized | Status: new
Component: contrib.admin | Version: 1.8
Severity: Normal | Resolution:
Keywords: id, model | Triage Stage:
inheritance, inherited model, | Unreviewed
inline |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Braintelligence):

* needs_docs: => 0
* needs_tests: => 0
* needs_better_patch: => 0


Old description:

> 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)
>
> class Author(Person):
> publisher = models.CharField(max_length=255)
> }}}
>
> I can't access the id field like so:
>
> {{{
> class AuthorInline(admin.StackedInline):
> model = Author
> fields = ['id', 'lastname', 'publisher']
>
> class SomethingAdmin(admin.ModelAdmin):
> resource_class = Something
> 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?

New description:

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']

class SomethingAdmin(admin.ModelAdmin):
resource_class = Something
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?

--

--
Ticket URL: <https://code.djangoproject.com/ticket/26502#comment:1>

Django

unread,
Apr 14, 2016, 7:42:29 AM4/14/16
to django-...@googlegroups.com
#26502: Lookup of attribute 'id' on model inheritance models in InlineModelAdmin
fails when using fk_name of parent model
-------------------------------------+-------------------------------------
Reporter: Braintelligence | Owner: nobody
Type: Uncategorized | Status: new
Component: contrib.admin | Version: 1.8
Severity: Normal | Resolution:
Keywords: id, model | Triage Stage:
inheritance, inherited model, | Unreviewed
inline |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Braintelligence:

Old description:

> 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']
>
> class SomethingAdmin(admin.ModelAdmin):
> resource_class = Something
> 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?

New description:

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']

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?

--

--
Ticket URL: <https://code.djangoproject.com/ticket/26502#comment:2>

Django

unread,
Apr 14, 2016, 9:25:53 AM4/14/16
to django-...@googlegroups.com
#26502: Lookup of attribute 'id' on model inheritance models in InlineModelAdmin
fails when using fk_name of parent model
-------------------------------------+-------------------------------------
Reporter: Braintelligence | Owner: nobody
Type: Uncategorized | Status: new
Component: contrib.admin | Version: 1.8
Severity: Normal | Resolution:
Keywords: id, model | Triage Stage:
inheritance, inherited model, | Unreviewed
inline |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by timgraham):

I think you need to use `person_ptr` in the `fields` list rather than
`id`. I created a [https://github.com/django/django/pull/6452 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."

--
Ticket URL: <https://code.djangoproject.com/ticket/26502#comment:3>

Django

unread,
Apr 15, 2016, 7:54:40 AM4/15/16
to django-...@googlegroups.com
#26502: Lookup of attribute 'id' on model inheritance models in InlineModelAdmin
fails when using fk_name of parent model
-------------------------------------+-------------------------------------
Reporter: Braintelligence | Owner: nobody
Type: Uncategorized | Status: new
Component: contrib.admin | Version: 1.8
Severity: Normal | Resolution:
Keywords: id, model | Triage Stage:
inheritance, inherited model, | Unreviewed
inline |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"3cb63b0e473568cb5158d7a4f13cb7e1c9ee89f5" 3cb63b0]:
{{{
#!CommitTicketReference repository=""
revision="3cb63b0e473568cb5158d7a4f13cb7e1c9ee89f5"
Refs #26502 -- Added choices to Form.__getitem__() KeyError message.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/26502#comment:4>

Django

unread,
Apr 15, 2016, 7:59:25 AM4/15/16
to django-...@googlegroups.com
#26502: Lookup of attribute 'id' on model inheritance models in InlineModelAdmin
fails when using fk_name of parent model
-------------------------------------+-------------------------------------
Reporter: Braintelligence | Owner: nobody
Type: Uncategorized | Status: closed
Component: Forms | Version: 1.8
Severity: Normal | Resolution: invalid

Keywords: id, model | Triage Stage:
inheritance, inherited model, | Unreviewed
inline |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* status: new => closed
* component: contrib.admin => Forms
* resolution: => invalid


--
Ticket URL: <https://code.djangoproject.com/ticket/26502#comment:5>

Django

unread,
Apr 15, 2016, 8:02:24 AM4/15/16
to django-...@googlegroups.com
#26502: Lookup of attribute 'id' on model inheritance models in InlineModelAdmin
fails when using fk_name of parent model
-------------------------------------+-------------------------------------
Reporter: Braintelligence | Owner: nobody
Type: Uncategorized | Status: closed
Component: Forms | Version: 1.8
Severity: Normal | Resolution: invalid
Keywords: id, model | Triage Stage:
inheritance, inherited model, | Unreviewed
inline |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Braintelligence):

Thanks a lot! :)

--
Ticket URL: <https://code.djangoproject.com/ticket/26502#comment:6>

Django

unread,
Apr 15, 2016, 10:27:26 AM4/15/16
to django-...@googlegroups.com
#26502: Lookup of attribute 'id' on model inheritance models in InlineModelAdmin
fails when using fk_name of parent model
-------------------------------------+-------------------------------------
Reporter: Braintelligence | Owner: nobody
Type: Uncategorized | Status: new

Component: Forms | Version: 1.8
Severity: Normal | Resolution:
Keywords: id, model | Triage Stage:
inheritance, inherited model, | Unreviewed
inline |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Braintelligence):

* status: closed => new
* resolution: invalid =>


Comment:

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 it appears at all. Why isn't it displayed?

--
Ticket URL: <https://code.djangoproject.com/ticket/26502#comment:7>

Django

unread,
Apr 15, 2016, 10:52:52 AM4/15/16
to django-...@googlegroups.com
#26502: Lookup of attribute 'id' on model inheritance models in InlineModelAdmin
fails when using fk_name of parent model
-------------------------------------+-------------------------------------
Reporter: Braintelligence | Owner: nobody
Type: Uncategorized | Status: closed
Component: Forms | Version: 1.8
Severity: Normal | Resolution: invalid

Keywords: id, model | Triage Stage:
inheritance, inherited model, | Unreviewed
inline |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* status: new => closed

* resolution: => invalid


Comment:

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

--
Ticket URL: <https://code.djangoproject.com/ticket/26502#comment:8>

Django

unread,
Apr 15, 2016, 11:14:26 AM4/15/16
to django-...@googlegroups.com
#26502: Lookup of attribute 'id' on model inheritance models in InlineModelAdmin
fails when using fk_name of parent model
-------------------------------------+-------------------------------------
Reporter: Braintelligence | Owner: nobody
Type: Uncategorized | Status: closed
Component: Forms | Version: 1.8
Severity: Normal | Resolution: invalid
Keywords: id, model | Triage Stage:
inheritance, inherited model, | Unreviewed
inline |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Braintelligence):

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?

--
Ticket URL: <https://code.djangoproject.com/ticket/26502#comment:9>

Django

unread,
Apr 15, 2016, 11:51:53 AM4/15/16
to django-...@googlegroups.com
#26502: Lookup of attribute 'id' on model inheritance models in InlineModelAdmin
fails when using fk_name of parent model
-------------------------------------+-------------------------------------
Reporter: Braintelligence | Owner: nobody
Type: Uncategorized | Status: closed
Component: Forms | Version: 1.8
Severity: Normal | Resolution: invalid
Keywords: id, model | Triage Stage:
inheritance, inherited model, | Unreviewed
inline |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by timgraham):

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

--
Ticket URL: <https://code.djangoproject.com/ticket/26502#comment:10>

Django

unread,
Apr 15, 2016, 12:12:42 PM4/15/16
to django-...@googlegroups.com
#26502: Lookup of attribute 'id' on model inheritance models in InlineModelAdmin
fails when using fk_name of parent model
-------------------------------------+-------------------------------------
Reporter: Braintelligence | Owner: nobody
Type: Uncategorized | Status: closed
Component: Forms | Version: 1.8
Severity: Normal | Resolution: invalid
Keywords: id, model | Triage Stage:
inheritance, inherited model, | Unreviewed
inline |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Braintelligence:

Old description:

> 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']
>
> 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?

New description:

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?

--

--
Ticket URL: <https://code.djangoproject.com/ticket/26502#comment:11>

Django

unread,
Apr 15, 2016, 12:14:35 PM4/15/16
to django-...@googlegroups.com
#26502: Lookup of attribute 'id' on model inheritance models in InlineModelAdmin
fails when using fk_name of parent model
-------------------------------------+-------------------------------------
Reporter: Braintelligence | Owner: nobody
Type: Uncategorized | Status: closed
Component: Forms | Version: 1.8
Severity: Normal | Resolution: invalid
Keywords: id, model | Triage Stage:
inheritance, inherited model, | Unreviewed
inline |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Braintelligence):

I changed the Inline class to reflect what I'm talking about.
The person_ptr field is not displayed in the admin form as a
readonly_field as well.

And even though it may not be a field that you are able to edit, using it
as readonly_field and not being able to see it is, from my point of view,
either a bug or doesn't follow the intuitive logic.

--
Ticket URL: <https://code.djangoproject.com/ticket/26502#comment:12>

Django

unread,
Apr 16, 2016, 8:56:07 PM4/16/16
to django-...@googlegroups.com
#26502: Lookup of attribute 'id' on model inheritance models in InlineModelAdmin
fails when using fk_name of parent model
-------------------------------------+-------------------------------------
Reporter: Braintelligence | Owner: nobody
Type: Uncategorized | Status: new

Component: Forms | Version: 1.8
Severity: Normal | Resolution:
Keywords: id, model | Triage Stage:
inheritance, inherited model, | Unreviewed
inline |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Braintelligence):

* status: closed => new
* resolution: invalid =>


--
Ticket URL: <https://code.djangoproject.com/ticket/26502#comment:13>

Django

unread,
Apr 18, 2016, 9:29:17 AM4/18/16
to django-...@googlegroups.com
#26502: Lookup of attribute 'id' on model inheritance models in InlineModelAdmin
fails when using fk_name of parent model
-------------------------------------+-------------------------------------
Reporter: Braintelligence | Owner: nobody
Type: Uncategorized | Status: closed
Component: Forms | Version: 1.8
Severity: Normal | Resolution: invalid

Keywords: id, model | Triage Stage:
inheritance, inherited model, | Unreviewed
inline |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* status: new => closed
* resolution: => invalid


Comment:

The issue is that the field uses a hidden widget by default so the HTML
uses a
[https://github.com/django/django/blob/2c4c67af94318b15df7d9d37b936e07e8168bc73/django/contrib/admin/templates/admin/includes/fieldset.html#L7
"hidden" CSS class] triggered by
[https://github.com/django/django/blob/2c4c67af94318b15df7d9d37b936e07e8168bc73/django/contrib/admin/helpers.py#L101-L104
Fieldline.has_visible_field]. You could likely change the widget in a
custom admin form to workaround this.

It might make sense to try to allow `readonly_fields` to override override
the widget automatically, but I'm not sure if a solution is feasible or if
it might cause some other problems. In any case, let's open a separate
ticket if you want to pursue such a solution since we've deviated quite a
bit from the original report. Thanks.

--
Ticket URL: <https://code.djangoproject.com/ticket/26502#comment:14>

Django

unread,
Apr 18, 2016, 11:43:14 AM4/18/16
to django-...@googlegroups.com
#26502: Lookup of attribute 'id' on model inheritance models in InlineModelAdmin
fails when using fk_name of parent model
-------------------------------------+-------------------------------------
Reporter: Braintelligence | Owner: nobody
Type: Uncategorized | Status: closed
Component: Forms | Version: 1.8
Severity: Normal | Resolution: invalid
Keywords: id, model | Triage Stage:
inheritance, inherited model, | Unreviewed
inline |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Braintelligence):

Thank you for clarifying this, I wouldn't have found out about this so
quick :).
I will try to create a custom admin form then and not pursue this any
further for now. Thank you again, very much.

--
Ticket URL: <https://code.djangoproject.com/ticket/26502#comment:15>

Reply all
Reply to author
Forward
0 new messages