Fixed #25531 -- Documented that admin_order_field supports lookups.
This commit is contained in:
parent
931b946e29
commit
2c72710111
|
@ -714,6 +714,22 @@ subclass::
|
||||||
|
|
||||||
colored_first_name.admin_order_field = '-first_name'
|
colored_first_name.admin_order_field = '-first_name'
|
||||||
|
|
||||||
|
``admin_order_field`` supports query lookups to sort by values on related
|
||||||
|
models. This example includes an "author first name" column in the list
|
||||||
|
display and allows sorting it by first name::
|
||||||
|
|
||||||
|
class Blog(models.Model):
|
||||||
|
title = models.CharField(max_length=255)
|
||||||
|
author = models.ForeignKey(Person, on_delete=models.CASCADE)
|
||||||
|
|
||||||
|
class BlogAdmin(admin.ModelAdmin):
|
||||||
|
list_display = ('title', 'author', 'author_first_name')
|
||||||
|
|
||||||
|
def author_first_name(self, obj):
|
||||||
|
return obj.author.first_name
|
||||||
|
|
||||||
|
author_first_name.admin_order_field = 'author__first_name'
|
||||||
|
|
||||||
* Elements of ``list_display`` can also be properties. Please note however,
|
* Elements of ``list_display`` can also be properties. Please note however,
|
||||||
that due to the way properties work in Python, setting
|
that due to the way properties work in Python, setting
|
||||||
``short_description`` on a property is only possible when using the
|
``short_description`` on a property is only possible when using the
|
||||||
|
|
Loading…
Reference in New Issue