Friday, November 30, 2012

[Django 1.3] list_display, __unicode__ and sorting

Hello,

Would the below be an OK way to sort __unicode__ in the admin list_display?

Model:

def uni_sort(self):

return self.__unicode__()

uni_sort.admin_order_field = 'sort'
uni_sort.short_description = _(u'name')

Admin:

list_display = ('uni_sort', 'parent', 'name', 'slug',)

...

To save ya'll some time, the docs say:

[[

The __str__() and __unicode__() methods are just as valid in
list_display as any other model method, so it's perfectly OK to do
this:

list_display = ('__unicode__', 'some_other_field')

Usually, elements of list_display that aren't actual database fields
can't be used in sorting (because Django does all the sorting at the
database level).

However, if an element of list_display represents a certain database
field, you can indicate this fact by setting the admin_order_field
attribute of the item.

]]

Unless I'm mistaken, this does not work:

__unicode__.admin_order_field = 'sort'

Otherwise I could just do this:

list_display = ('__unicode__', 'parent', 'name', 'slug',)

My question:

Is it overkill to create a method, that returns the __unicode__ value,
just for the sake of being able to order on the "sort" field?

Thanks!

Cheers,
M

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment