{{{
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.
* 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>
--
Ticket URL: <https://code.djangoproject.com/ticket/25134#comment:2>
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>
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>
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>
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>
* status: new => closed
* resolution: => duplicate
Comment:
Duplicate of #16117
--
Ticket URL: <https://code.djangoproject.com/ticket/25134#comment:7>
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>