[Django] #34767: AdminReadonlyField may not show links to related models when the related model has been downcasted

6 views
Skip to first unread message

Django

unread,
Aug 8, 2023, 3:01:34 PM8/8/23
to django-...@googlegroups.com
#34767: AdminReadonlyField may not show links to related models when the related
model has been downcasted
-------------------------------------------+------------------------
Reporter: matthias-margin | Owner: nobody
Type: Uncategorized | Status: new
Component: contrib.admin | Version: 4.2
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 1
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 1 |
-------------------------------------------+------------------------
`get_admin_url` uses the `remote_field.model` to generate the view name to
use to link to the model. However, the model can be downcasted into a
different class (eg by using TypedModels). If this downcasted class is the
one that has an admin registered and the base class does not, then the
link is not rendered.

Fortunately, the `get_admin_url` method already gets the instance of the
model (`remote_obj`), which can be used instead to generate the view name.


{{{
diff --git a/django/contrib/admin/helpers.py
b/django/contrib/admin/helpers.py
index 90ca7affc8..25c1f89a71 100644
--- a/django/contrib/admin/helpers.py
+++ b/django/contrib/admin/helpers.py
@@ -243,10 +243,10 @@ class AdminReadonlyField:
self.form.label_suffix,
)

- def get_admin_url(self, remote_field, remote_obj):
+ def get_admin_url(self, remote_obj):
url_name = "admin:%s_%s_change" % (
- remote_field.model._meta.app_label,
- remote_field.model._meta.model_name,
+ remote_obj._meta.app_label,
+ remote_obj._meta.model_name,
)
try:
url = reverse(
@@ -292,7 +292,7 @@ class AdminReadonlyField:
isinstance(f.remote_field, (ForeignObjectRel,
OneToOneField))
and value is not None
):
- result_repr = self.get_admin_url(f.remote_field,
value)
+ result_repr = self.get_admin_url(value)
else:
result_repr = display_for_field(value, f,
self.empty_value_display)
result_repr = linebreaksbr(result_repr)
}}}

this change should be a no-op change most everyone.

I tried to add tests but could not find where this was being tested. Happy
to put out a pull request in github is someone can point me to the right
place to update tests.

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

Django

unread,
Aug 8, 2023, 3:04:25 PM8/8/23
to django-...@googlegroups.com
#34767: AdminReadonlyField may not show links to related models when the related
model has been downcasted
-------------------------------------+-------------------------------------
Reporter: matthias-margin | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: contrib.admin | Version: 4.2
Severity: Normal | Resolution:

Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 1
-------------------------------------+-------------------------------------
Changes (by matthias-margin):

* type: Uncategorized => Cleanup/optimization


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

Django

unread,
Aug 8, 2023, 4:30:14 PM8/8/23
to django-...@googlegroups.com
#34767: AdminReadonlyField may not show links to related models when the related
model has been downcasted
-------------------------------------+-------------------------------------
Reporter: matthias-margin | Owner: nobody
Type: | Status: closed

Cleanup/optimization |
Component: contrib.admin | Version: 4.2
Severity: Normal | Resolution: needsinfo
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 1
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* has_patch: 1 => 0
* resolution: => needsinfo


Comment:

> ''"... can be downcasted into a different class (eg by using
TypedModels)"''

Sorry for my ignorance, but I don't understand what it means. Can you
attached a sample project that reproduces the issue?

> ''"If this downcasted class is the one that has an admin registered and
the base class does not, then the link is not rendered. "''

This sounds really niche, and would probably cause issues in many places.

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

Reply all
Reply to author
Forward
0 new messages