#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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/01070189d6869266-ca636fdc-841b-4e61-8d77-fb3dbe1373cb-000000%40eu-central-1.amazonses.com.

Reply via email to