Opened 15 months ago
Closed 15 months ago
#34767 closed Cleanup/optimization (needsinfo)
AdminReadonlyField may not show links to related models when the related model has been downcasted
Reported by: | matthias-margin | Owned by: | nobody |
---|---|---|---|
Component: | contrib.admin | Version: | 4.2 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | yes |
Description
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.
Change History (2)
comment:1 by , 15 months ago
Type: | Uncategorized → Cleanup/optimization |
---|
comment:2 by , 15 months ago
Has patch: | unset |
---|---|
Resolution: | → needsinfo |
Status: | new → closed |
Sorry for my ignorance, but I don't understand what it means. Can you attached a sample project that reproduces the issue?
This sounds really niche, and would probably cause issues in many places.