[Django] #36804: AdminSite.get_model_admin raises AttributeError when `to` model of a ForeignKey is defined as a string

2 views
Skip to first unread message

Django

unread,
7:09 AM (11 hours ago) 7:09 AM
to django-...@googlegroups.com
#36804: AdminSite.get_model_admin raises AttributeError when `to` model of a
ForeignKey is defined as a string
--------------------------------+-----------------------------------------
Reporter: Parth Paradkar | Type: Bug
Status: new | Component: contrib.admin
Version: 5.2 | Severity: Normal
Keywords: admin | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------+-----------------------------------------
I have a foreign key on one of my models defined as- \\


{{{
slack_alert = models.ForeignKey(
"falcon.SlackAlert",
on_delete=models.SET_NULL,
null=True,
blank=True,
related_name="tickets",
)
}}}



When I do not include the `falcon` app in `INSTALLED_APPS`, the admin site
raises an error (which is expected). However, while handling the expected
`KeyError`, it throws an `AttributeError`. I am guessing this is since it
expects a `Model` instance.

Traceback-


{{{
Traceback (most recent call last):
File "/Users/parthparadkar/projects/lighthouse/venv/lib/python3.11/site-
packages/django/contrib/admin/sites.py", line 170, in get_model_admin
return self._registry[model]
~~~~~~~~~~~~~~^^^^^^^
KeyError: 'falcon.SlackAlert'

During handling of the above exception, another exception occurred:

File "/Users/parthparadkar/projects/lighthouse/venv/lib/python3.11/site-
packages/django/contrib/admin/checks.py", line 209, in
_check_autocomplete_fields
[
File "/Users/parthparadkar/projects/lighthouse/venv/lib/python3.11/site-
packages/django/contrib/admin/checks.py", line 210, in <listcomp>
self._check_autocomplete_fields_item(
File "/Users/parthparadkar/projects/lighthouse/venv/lib/python3.11/site-
packages/django/contrib/admin/checks.py", line 239, in
_check_autocomplete_fields_item
related_admin =
obj.admin_site.get_model_admin(field.remote_field.model)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/parthparadkar/projects/lighthouse/venv/lib/python3.11/site-
packages/django/contrib/admin/sites.py", line 172, in get_model_admin
raise NotRegistered(f"The model {model.__name__} is not registered.")
^^^^^^^^^^^^^^
AttributeError: 'str' object has no attribute '__name__'. Did you mean:
'__ne__'?

}}}


I think this can be fixed by checking with `isinstance` before raising
`NotRegistered`?

{{{
except KeyError:
if isinstance(model, str):
raise NotRegistered(f"The model {model} is not registered.")
else:
raise NotRegistered(f"The model {model.__name__} is not
registered.")
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36804>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
9:27 AM (8 hours ago) 9:27 AM
to django-...@googlegroups.com
#36804: AdminSite.get_model_admin raises AttributeError when `to` model of a
ForeignKey is defined as a string
--------------------------------------+------------------------------------
Reporter: Parth Paradkar | Owner: (none)
Type: Cleanup/optimization | Status: new
Component: contrib.admin | Version: 5.2
Severity: Normal | Resolution:
Keywords: admin | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Jacob Walls):

* stage: Unreviewed => Accepted
* type: Bug => Cleanup/optimization

Comment:

Thanks. I'd suggest `{model!r}` instead of `{model}` but otherwise LGTM.
--
Ticket URL: <https://code.djangoproject.com/ticket/36804#comment:1>

Django

unread,
9:36 AM (8 hours ago) 9:36 AM
to django-...@googlegroups.com
#36804: AdminSite.get_model_admin raises AttributeError when `to` model of a
ForeignKey is defined as a string
-------------------------------------+-------------------------------------
Reporter: Parth Paradkar | Owner: Parth
Type: | Paradkar
Cleanup/optimization | Status: assigned
Component: contrib.admin | Version: 5.2
Severity: Normal | Resolution:
Keywords: admin | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Parth Paradkar):

* owner: (none) => Parth Paradkar
* status: new => assigned

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

Django

unread,
12:52 PM (5 hours ago) 12:52 PM
to django-...@googlegroups.com
#36804: AdminSite.get_model_admin raises AttributeError when `to` model of a
ForeignKey is defined as a string
-------------------------------------+-------------------------------------
Reporter: Parth Paradkar | Owner: Parth
Type: | Paradkar
Cleanup/optimization | Status: assigned
Component: contrib.admin | Version: 5.2
Severity: Normal | Resolution:
Keywords: admin | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Parth Paradkar):

* has_patch: 0 => 1

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

Django

unread,
1:06 PM (5 hours ago) 1:06 PM
to django-...@googlegroups.com
#36804: AdminSite.get_model_admin raises AttributeError when `to` model of a
ForeignKey is defined as a string
-------------------------------------+-------------------------------------
Reporter: Parth Paradkar | Owner: Parth
Type: | Paradkar
Cleanup/optimization | Status: assigned
Component: contrib.admin | Version: dev
Severity: Normal | Resolution:
Keywords: admin | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* stage: Accepted => Ready for checkin
* version: 5.2 => dev

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

Django

unread,
1:25 PM (5 hours ago) 1:25 PM
to django-...@googlegroups.com
#36804: AdminSite.get_model_admin raises AttributeError when `to` model of a
ForeignKey is defined as a string
-------------------------------------+-------------------------------------
Reporter: Parth Paradkar | Owner: Parth
Type: | Paradkar
Cleanup/optimization | Status: assigned
Component: contrib.admin | Version: dev
Severity: Normal | Resolution:
Keywords: admin | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* needs_better_patch: 0 => 1
* stage: Ready for checkin => Accepted

--
Ticket URL: <https://code.djangoproject.com/ticket/36804#comment:5>
Reply all
Reply to author
Forward
0 new messages