Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django admin search/filter functionality as a page table

I was wondering if there is a way to use the power of Django Admin's filtering/ordering/paginating/search capabilities in a regular view.

What I mean is that I have a model, some fields on it. I'd like to have a "search" form, where fields would be defined much like using admin.ModelAdmin. User would be able to search (using provided fields), filter by values, paginate through pages of result table etc. All that without minimal amount of work on my part, eg. just configuration which fields should be used in the form. Something like this:

class SchoolAdmin(ModelAdmin):
    list_display = ('id', 'name', )
    list_display_links = ('name', )
    search_fields = ('name', )
    list_filter = ('type', )

Is there something like this available? Or do I have to code it myself?

Edit:

Features I require from such a plugin/application are:

  1. Display data as a table
  2. Sorting by columns
  3. Filtering (eg. "show only rows that has X = Y")
  4. Searching by columns
  5. Optionally configuration similar to ModelAdmin style

Alasdair's django-tables2 only matches 1st and 2nd conditions.

like image 288
Ondrej Skalicka Avatar asked Nov 03 '11 17:11

Ondrej Skalicka


2 Answers

The django functionality you mention isn't really reusable in custom views as of Django 1.3. There was recently some discussion on the django-developers group about splitting out admin functionality to make it reusable.

I have come across two projects that might be useful to you, django-tables2 and django-filter. They both offer slightly different things, I think you're looking for a mixture of the two.

django-tables2

django-tables2 simplifies the task of turning sets of data into HTML tables. It has native support for pagination and sorting. It does for HTML tables what django.forms does for HTML forms

django-filter

Django-filter is a reusable Django application for allowing users to filter queryset dynamically. It requires Python 2.4 or higher. For usage and installation instructions consult the docs directory.

Django-filter can be used for generating interfaces similar to the Django admin's list_filter interface. It has an API very similar to Django's ModelForms.

like image 99
Alasdair Avatar answered Oct 18 '22 07:10

Alasdair


To extend on Alasdair's answer, I added a simple module that adds simple filtering to django-tables2. See https://github.com/benjiec/django-tables2-simplefilter.

like image 24
Overclocked Avatar answered Oct 18 '22 08:10

Overclocked