I keep numeric fields like "size", "width", "height" in my database. Now I would attach units like "KiB" or "pixels" to them when showing them in the change list. This could easily be achieved by adding callables such as "size_formatted" etc to list_display. However, these are no longer sortable.
Is there a way around this limitation?
You can order the fields as you wish using the ModelAdmin. fields option. The order of fields would depend on the order in which you declare them in your models. So alternatively you could order the fields in the way you would want them to show in the admin.
editable=False will make the field disappear from all forms including admin and ModelForm i.e., it can not be edited using any form.
Overview. The Django admin application can use your models to automatically build a site area that you can use to create, view, update, and delete records. This can save you a lot of time during development, making it very easy to test your models and get a feel for whether you have the right data.
Read here - ModelAdmin.list_display (read a lot to get to the point ;) )
you need to add admin_order_field attribute to your function
class YourAdminCLass(admin.ModelAdmin)
[...]
def size_formatted(self, obj):
return "whatever you need"
size_formatted.admin_order_field = 'size'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With