python - Django admin change list view disable sorting for some fields -
is there way(s) disable sorting function fields in django admin change list fields, users cannot click column header sort list.
i tried on following method, doesn't work.
https://djangosnippets.org/snippets/2580/
i tired override changelist_view in modeladmin nothing happen.
def changelist_view(self, request, extra_context=none): self.ordering_fields = ['id'] return super(mymodeladmin, self).changelist_view(request, extra_context)
in above case, allow user sort list id.
anyone has suggestion? thanks.
for django 1.7 (or version last use) not support such things. 1 possible dirty work-around defining model class method , using method instead of model field.
class testclass(model): some_field = (.....) other_field = (........) def show_other_field(self): return self.other_field class testclassadmin(modeladmin): list_display = ("some_field", "show_other_field")
since show_other_field
model class method, django not knows how sort (or process) return result of method.
but said, dirty hack might require more processing (and maybe more database calls) according use-case displaying field of model.
extra: if want make model method sortable, must pass admin_order_field
value like:
def show_other_field(self): return self.other_field show_other_field.admin_order_field = "other_field"
that make model method sortable in admin list_display
. have pass field or relation usable in order_by
method of database api.
testclass.objects.filter(....).order_by(<admin_order_field>)
Comments
Post a Comment