Django admin: format fields in a list, but keep sorting?

I store numerical fields such as "size", "width", "height" in my database. Now I added units to them, such as โ€œKiBโ€ or โ€œpixelsโ€, when I showed them in the change list. This can be easily achieved by adding entries to the list_display list, such as "size_formatted", etc. However, they are no longer sorted.

Is there any way to limit this restriction?

+3
source share
1 answer

Read here - ModelAdmin.list_display (read a lot to get to the point;))

admin_order_field

class YourAdminCLass(admin.ModelAdmin)
   [...]
   def size_formatted(self, obj):
          return "whatever you need"
   size_formatted.admin_order_field = 'size'
+8

Source: https://habr.com/ru/post/1724548/


All Articles