Opened 4 years ago
Closed 4 years ago
#32804 closed New feature (wontfix)
Add 'link' attribute to admin display function to enable custom link
Reported by: | Skrattoune | Owned by: | nobody |
---|---|---|---|
Component: | contrib.admin | Version: | 3.2 |
Severity: | Normal | Keywords: | admin, hyperlink, decorator, display |
Cc: | pope1ni | Triage Stage: | Unreviewed |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
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()
Note:
See TracTickets
for help on using tickets.
Thanks for this proposition, however IMO we have enough ways to customize the admin changelist. The Django Admin is not a universal tool for building an app. You can create your own decorator if you need a shortcut for this.