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

11 views
Skip to first unread message

Django

unread,
Dec 16, 2025, 7:09:18 AM12/16/25
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,
Dec 16, 2025, 9:27:37 AM12/16/25
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,
Dec 16, 2025, 9:36:00 AM12/16/25
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,
Dec 16, 2025, 12:52:21 PM12/16/25
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,
Dec 16, 2025, 1:06:32 PM12/16/25
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,
Dec 16, 2025, 1:25:52 PM12/16/25
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>

Django

unread,
Dec 16, 2025, 11:39:14 PM12/16/25
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
-------------------------------------+-------------------------------------
Comment (by Raghav-Bodani):

I can reproduce this and would like to work on a fix with a regression
test. Please let me know if that’s an issue.
--
Ticket URL: <https://code.djangoproject.com/ticket/36804#comment:6>

Django

unread,
Dec 27, 2025, 10:00:54 AM12/27/25
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: 1
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Parth Paradkar):

* stage: Accepted => Ready for checkin

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

Django

unread,
Dec 29, 2025, 9:50:05 AM12/29/25
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: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

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

Comment:

(When requesting a review, unset the Needs Improvement checkbox.)
--
Ticket URL: <https://code.djangoproject.com/ticket/36804#comment:8>

Django

unread,
Dec 29, 2025, 1:39:34 PM12/29/25
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: 1 | Patch needs improvement: 1
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* needs_better_patch: 0 => 1
* needs_tests: 0 => 1

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

Django

unread,
Jan 8, 2026, 4:14:49 PMJan 8
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: 1 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* needs_better_patch: 1 => 0

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

Django

unread,
Jan 11, 2026, 9:38:15 AMJan 11
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: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Parth Paradkar):

* needs_tests: 1 => 0

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

Django

unread,
Jan 12, 2026, 8:44:51 AM (13 days ago) Jan 12
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

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

Django

unread,
Jan 12, 2026, 8:46:34 AM (13 days ago) Jan 12
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: closed
Component: contrib.admin | Version: dev
Severity: Normal | Resolution: fixed
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 GitHub <noreply@…>):

* resolution: => fixed
* status: assigned => closed

Comment:

In [changeset:"eeea8d2cba41229b10226d8b9be184f0b015e95c" eeea8d2]:
{{{#!CommitTicketReference repository=""
revision="eeea8d2cba41229b10226d8b9be184f0b015e95c"
Fixed #36804 -- Fixed admin system check crash for missing models.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36804#comment:13>
Reply all
Reply to author
Forward
0 new messages