[Django]-Django – admin_order_field with custom non-related object

7👍

Basically, if you can refer to something the way you do a field, you can use it in admin_order_field.

So, if you wanted to order by the Card's serial_number you could say `admin_order_field = ‘card__serial_number’.

Or, if you wanted to use annotations, you could do that by first defining a get_queryset() method that creates the annotation; and then using the new annotation name as your admin_order_field.

But in your case, you can’t do it. The problem is the restriction to type="xxx"; there’s no way to represent that logic using the field notation.

It might be possible, though, to use extra() to define a custom field with the information you want, and then use that as your admin_order_field.

Leave a comment