[Django] #25134: Add a list_display_method decorator

35 views
Skip to first unread message

Django

unread,
Jul 17, 2015, 7:56:46 AM7/17/15
to django-...@googlegroups.com
#25134: Add a list_display_method decorator
-------------------------------+-------------------------------------------
Reporter: jaap3 | Owner: nobody
Type: New feature | Status: new
Component: contrib.admin | Version: master
Severity: Normal | Keywords: list_display method decorator
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+-------------------------------------------
While doing some heavy admin customisation I got fed up repeating function
names to add `short_description`, `admin_order_field` (etc.) to helpers
used in `list_display`. So I created a decorator that does it for me. It
has the added benefit of enabling autocompletion on the possible options
for `list_display` methods in PyCharm.

{{{
def list_display_method(short_description=None, allow_tags=False,
boolean=False, admin_order_field=None):
"""
Convenient decorator for display functions used in admin list_display.
"""
def inner(func):
func.short_description = short_description or func.__name__
func.allow_tags = allow_tags
func.boolean = boolean
func.admin_order_field = admin_order_field
return func
return inner
}}}

Used like this:

{{{
@list_display_method(_('Some description'),
admin_order_field='other_field')
def show_other_field(obj):
return obj.other_field
}}}

Would something like this be eligible for inclusion in `contrib.admin`?

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

Django

unread,
Jul 17, 2015, 9:59:30 AM7/17/15
to django-...@googlegroups.com
#25134: Add a list_display_method decorator
-------------------------------------+-------------------------------------

Reporter: jaap3 | Owner: nobody
Type: New feature | Status: new
Component: contrib.admin | Version: master
Severity: Normal | Resolution:
Keywords: list_display method | Triage Stage: Accepted
decorator |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* needs_better_patch: => 0
* stage: Unreviewed => Accepted
* needs_tests: => 0
* needs_docs: => 0


Comment:

Sounds reasonable to me. Maybe a more generic name like `@admin_method()`
would be better since these items can be used on more than just the
changelist page? I think we could also drop the `admin_` prefix on
`order_field` in the decorator's signature too.

If we're able to deprecate `allow_tags` in #25135, then we should omit
that here.

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

Django

unread,
Jul 17, 2015, 10:00:06 AM7/17/15
to django-...@googlegroups.com
#25134: Add a decorator to escapsulate admin method properties
-------------------------------------+-------------------------------------

Reporter: jaap3 | Owner: nobody
Type: New feature | Status: new
Component: contrib.admin | Version: master
Severity: Normal | Resolution:
Keywords: list_display method | Triage Stage: Accepted
decorator |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

--
Ticket URL: <https://code.djangoproject.com/ticket/25134#comment:2>

Django

unread,
Jul 20, 2015, 11:13:05 AM7/20/15
to django-...@googlegroups.com
#25134: Add a decorator to escapsulate admin method properties
-------------------------------------+-------------------------------------
Reporter: jaap3 | Owner: nobody
Type: New feature | Status: new
Component: contrib.admin | Version: master
Severity: Normal | Resolution:
Keywords: list_display method | Triage Stage: Accepted
decorator |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by jaap3):

Maybe `@display_method`? `@admin_method` feels a bit too generic to me.

Having looked at the code in `admin_list.items_for_result` it seems that
exposing `row_classes` and `empty_value_display` would be useful as well.

--
Ticket URL: <https://code.djangoproject.com/ticket/25134#comment:3>

Django

unread,
Jul 20, 2015, 11:51:39 AM7/20/15
to django-...@googlegroups.com
#25134: Add a decorator to escapsulate admin method properties
-------------------------------------+-------------------------------------
Reporter: jaap3 | Owner: nobody
Type: New feature | Status: new
Component: contrib.admin | Version: master
Severity: Normal | Resolution:
Keywords: list_display method | Triage Stage: Accepted
decorator |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by timgraham):

I'm not enthusiastic about putting more admin attributes in `models.py`.
While it might not be worthwhile to deprecate the existing customization
that can be done there, I think any further customization should live in
`ModelAdmin`. For that reason, we might consider closing this ticket as
"won't fix" as such a decorator would add a dependency in `models.py` on
`contrib.admin` (I presume the decorator would live in there).

--
Ticket URL: <https://code.djangoproject.com/ticket/25134#comment:4>

Django

unread,
Jul 20, 2015, 2:39:18 PM7/20/15
to django-...@googlegroups.com
#25134: Add a decorator to escapsulate admin method properties
-------------------------------------+-------------------------------------
Reporter: jaap3 | Owner: nobody
Type: New feature | Status: new
Component: contrib.admin | Version: master
Severity: Normal | Resolution:
Keywords: list_display method | Triage Stage: Accepted
decorator |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by jaap3):

Not sure if I follow you. In the case of `list_display` it can be a
standalone function, a method on the ModelAdmin or an attribute of the
model.
https://docs.djangoproject.com/en/1.8/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_display

The same goes for `readonly_fields` except that it can't be a callable
directly (is that right?)
https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.readonly_fields

I personally prefer creating these display methods on the applicable admin
class. That's where I've used this decorator and would use this decorator.

I guess there's nothing to stop people from using it in models.py. To
discourage people from using this in models the docs could maybe
deemphasise the models.py use cases? Most of the examples could be
rewritten to a standalone function or a method on the ModelAdmin.

--
Ticket URL: <https://code.djangoproject.com/ticket/25134#comment:5>

Django

unread,
Jul 20, 2015, 2:52:30 PM7/20/15
to django-...@googlegroups.com
#25134: Add a decorator to escapsulate admin method properties
-------------------------------------+-------------------------------------
Reporter: jaap3 | Owner: nobody
Type: New feature | Status: new
Component: contrib.admin | Version: master
Severity: Normal | Resolution:
Keywords: list_display method | Triage Stage: Accepted
decorator |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by timgraham):

Okay, sorry I forgot about that use case. That seems fine. I would make an
initial implementation using the existing attributes as options, then we
can talk about exposing the other attributes you mentioned.

--
Ticket URL: <https://code.djangoproject.com/ticket/25134#comment:6>

Django

unread,
Aug 24, 2015, 8:14:52 AM8/24/15
to django-...@googlegroups.com
#25134: Add a decorator to escapsulate admin method properties
-------------------------------------+-------------------------------------
Reporter: jaap3 | Owner: nobody
Type: New feature | Status: closed
Component: contrib.admin | Version: master
Severity: Normal | Resolution: duplicate

Keywords: list_display method | Triage Stage: Accepted
decorator |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* status: new => closed
* resolution: => duplicate


Comment:

Duplicate of #16117

--
Ticket URL: <https://code.djangoproject.com/ticket/25134#comment:7>

Django

unread,
Jan 13, 2021, 11:19:47 AM1/13/21
to django-...@googlegroups.com
#25134: Add a decorator to escapsulate admin method properties
-------------------------------------+-------------------------------------
Reporter: Jaap Roes | Owner: nobody
Type: New feature | Status: closed
Component: contrib.admin | Version: master

Severity: Normal | Resolution: duplicate
Keywords: list_display method | Triage Stage: Accepted
decorator |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by GitHub <noreply@…>):

In [changeset:"920448539631b52dcee53bd32a880abbc9de18bd" 9204485]:
{{{
#!CommitTicketReference repository=""
revision="920448539631b52dcee53bd32a880abbc9de18bd"
Fixed #16117 -- Added decorators for admin action and display functions.

Refs #25134, #32099.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/25134#comment:8>

Reply all
Reply to author
Forward
0 new messages