[Django] #32804: Add 'link' attribute to admin display function to enable custom link

4 views
Skip to first unread message

Django

unread,
Jun 1, 2021, 7:00:32 AM6/1/21
to django-...@googlegroups.com
#32804: Add 'link' attribute to admin display function to enable custom link
-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
Skrattoune |
Type: New | Status: new
feature |
Component: | Version: 3.2
contrib.admin | Keywords: admin, hyperlink,
Severity: Normal | decorator, display
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Hi, The new admin.display decorator is great for decreasing the burden for
declaring custom admin pages.

What would be even greater for my personal django usage, would be to add
directly from the display decorator the possibility to associate a custom
link to the displayed attribute.

For instance, instead of having to declare in the AdminModel:
{{{
@admin.display(ordering='-nb_variants', description='nb_variants')
def _nb_variants(self, obj):
# adding a link to open a new tab with the result
return format_html(f'<a
href="/admin/puzzles/puzzle/?uncolored_puzzle_id={obj.uncolored_puzzle_id}"
target="_blank">{obj.nb_variants}</a>')
}}}

to enable declaring directly inside the model at the property or method
level:

{{{

def href_to_variants(self):
return
f"/admin/puzzles/puzzle/?uncolored_puzzle_id={self.uncolored_puzzle_id}"

@cached_property
@admin.display(
link='href_to_variants',
target_blank=True,
)
def nb_variants(self):
return self.variants.count()

}}}

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

Django

unread,
Jun 1, 2021, 9:30:38 AM6/1/21
to django-...@googlegroups.com
#32804: Add 'link' attribute to admin display function to enable custom link
-------------------------------------+-------------------------------------
Reporter: Skrattoune | Owner: nobody
Type: New feature | Status: closed
Component: contrib.admin | Version: 3.2
Severity: Normal | Resolution: wontfix
Keywords: admin, hyperlink, | Triage Stage:
decorator, display | Unreviewed
Has patch: 0 | Needs documentation: 0

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

* cc: Nick Pope (added)
* status: new => closed
* resolution: => wontfix


Comment:

Thanks for this proposition, however IMO we have enough ways to customize
the admin changelist. The Django Admin is not a
[https://docs.djangoproject.com/en/3.2/ref/contrib/admin/#module-
django.contrib.admin universal tool for building an app]. You can create
your own decorator if you need a shortcut for this.

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

Reply all
Reply to author
Forward
0 new messages