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.
* type: Uncategorized => Cleanup/optimization
--
Ticket URL: <https://code.djangoproject.com/ticket/34767#comment:1>
* 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>