using django 1.7.7 admin page, want list data in table can sort , filter. that's picture on documentation shows: https://docs.djangoproject.com/en/1.7/ref/contrib/admin/ under list_filter section. tried , got error: value of 'list_display[0]' refers 'book_type', not callable. then, tried this:
# model.py: class interp(): """ specifies interpretation model """ book_types = ['drama', 'scietific'] book_types = tuple(zip(book_types, book_types)) comment = models.charfield(max_length=20) book_type = models.charfield(max_length=20, choices=book_types, default='not specified') def __unicode__(self): return self.book_type # admin.py: class booktypefilter(simplelistfilter): title = 'book type' parameter_name = 'book_type' def lookups(self, request, model_admin): types = set([t.book_type t in model_admin.model.objects.all()]) return zip(types, types) def queryset(self, request, queryset): if self.value(): return queryset.filter(book_type__id__exact=self.value()) else: return queryset class admininterpstore(admin.modeladmin): """ admin page setting """ search_fields = ('comment', 'book_type') list_display = ('book_type',) list_filter = (booktypefilter, )
this shows sidebar , lists values click of them, error:
fielderror: unsupported lookup 'id' charfield or join on field not permitted
- how can make filter, preferably table view well?
- is complete sample django1.7.7 can @ filtering admin page?
you must replace
return queryset.filter(book_type__id__exact=self.value())
by
return queryset.filter(book_type=self.value())
see https://docs.djangoproject.com/en/1.8/ref/models/querysets/#field-lookups more information.
also, in lookups
method, using:
types = model_admin.model.objects.distinct('book_type').values_list('book_type', flat=true)
would more efficient not retrieve whole data database filter it.
edit: realized book_type
field choice field.
first, value passed choices
argument must be
an iterable (e.g., list or tuple) consisting of iterables of 2 items (e.g. [(a, b), (a, b) ...] being value stored in database, b human readable value)
so passing list of strings fail.
second, django admin's default behaviour such fields trying achieve setting list_filter = ('book_type', )
in admininterpstore
enough.