Under the documentation for ModelAdmin.list_display
it describes a few ways to configure a method/function for usage and display in an admin's list view:
admin_order_field
(describes which field in the model to use for ordering by the method)allow_tags
(allows HTML to be displayed rather than escaped)short_description
(sets the label for the column)boolean
(determines if the field should be treated a boolean field for display)
It describes them as method attributes.
Addendum
Just found some more method/function attributes, used for template filters:
is_safe
, used when marking a template filter as safeneeds_autoescape
, used to deal with autoescaping of data
What other method attributes are there in Django (or even Python)? Or are these really the only cases?
Clarification
Just to be clear, this is what I'm talking about specifically.
In the following code:
class Foo(models.Model):
name = models.CharField(max_length=100)
color = models.CharField(max_length=100)
age = models.PositiveIntegerField()
def is_adult(self):
return age > 18
is_adult.boolean = True
is_adult.short_description = "Over 18?"
def colored_name(self):
return '<span style="color: %s">%s</span>' % (self.color, self.name)
colored_name.allow_tags = True
colored_name.short_desciption = "Name"
colored_name.admin_order_field = "name"
The method attributes I'm talking about are is_adult.boolean
, is_adult.short_description
, colored_name.allow_tags
, colored_name.short_description
and colored_name.admin_order_field
.
If you need any more details, please read the linked documentation.
Addendum #2
Looks like this is partially covered in PEP 232: Function Attributes. The PEP points to a mailing list post that lists other potential use cases for function attributes:
I need to associate a Java-style type declaration with a method so that it can be recognized based on its type during Java method dispatch. How would you do that with instances?
I need to associate a "grammar rule" with a Python method so that the method is invoked when the parser recognizes a syntactic construct in the input data.
I need to associate an IDL declaration with a method so that a COM interface definition can be generated from the source file.
I need to associate an XPath "pattern string" with a Python method so that the method can be invoked when a tree walker discovers a particular pattern in an XML DOM.
I need to associate multiple forms of documentation with a method. They are optimized for different IDEs, environments or languages.
Here's an implementation that allows method attributes to be callable:
from django.contrib.admin import ModelAdmin
from datetime.datetime import now
class ProfileAdmin(ModelAdmin):
list_display = ('votes_today',)
class VotesToday:
def __call__(self, model_admin, obj):
today = now().replace(hour=0, minute=0, second=0, microsecond=0)
return obj.vote_set.filter(created__gte=today)
@property
def short_description(self):
return 'Votes today (%s)' % now().strftime('%B %d')
@property
def votes_today(self):
if not hasattr(self, '__votes_today'):
self.__votes_today = self.VotesToday()
return self.__votes_today