On Mon, Mar 25, 2013 at 09:48:08AM -0700, Florian Apolloner wrote:
> No real plan yet and I don't know of any efforts either.
I have forked django-filter one month ago and patched it to
integrate with the admin. The real reason I have not contributed back is
that I have not had the time to create tests (while I did integrate docs).
I can share the code if someone is interested and I can create some patches.
In my case the complete admin integration is split in two different packages.
The goal of my fork was to create an easy way to add filters in ModelAdmin,
simply setting an attribute that I named 'advanced_search_fields'.
In the following, 'advanced_search_fields' refers to a filter that allows to
filter the fields independently and is clearly implemented with a FilterSet
form.
In my current implementation, the presence of attribute
'advanced_search_fields' on ModelAdmin generates a button that will display
the form filter when cliecked.
Below you can find the features I added to django-filters:
short way to declare lookup_type
--------------------------------
I realized that in many circumstancies I needed to create a FilterSet class
just to set the lookup_type for each field, so I modified it in a way that
accept the lookup_type to be added to the field_name::
class CertificateAdmin(ModelAdmin):
model = Certificates
advanced_search_fields = (
('start_date__gte', 'start_date__range', 'user'),
('status__in', 'description__icontains',),
)
status__in
----------
in case the field has choices, I fill the widget with them. In case the
lookup_type is 'in' I set a MultipleChoices
boolean
--------
Bolean fields has a default that is '------'
date range
-----------
When the lookp type is 'range' I use the DateRangeFilter. This definetely
reflects a personal choice.
Implementation
==============
My working implementation to get advance_search in admin pages is split
in different places:
:change_list.html: added 2 templatetags:
* one to add a button "advanced search" if needed
* one to add the form with advanced search input filters
* some javascript to serialize the form (just filled in fields)
:ModelAdmin: added/customized several methods:
* lookup_allowed: must let any field present in 'advanced_search_fields' to
be used
* setup_advanced_search: create the FilterSet based on the declared
fields and set it as 'search_filterset'. To allow for greater
flexibility, it's possible to declare an advanced_search_filterset
instead.
* get_changelist: a personalized changelist is used (see below)
* queryset: if an advanced_search is requested, the new queryset produced
by FilterSet (search_filterset.qs) is returned
:ChangeList: currently I customized the 'get_filters' method, to clean
self.params from filters already used.
:templatetags:
* jsearch_form: adds "Advanced Search" button
* advanced_search_form: renders the html form
Conclusions
============
What I described aove is a working setup that I already have in production,
but I'm sure it would need some ironing before considering it for
django-filters integration. As you can see it touches several different parts
but if django-filters should integrate templates/ModelAdmin could be as easy
as declaring your ModelAdmin as descendent of django-filter's ModelAdmin and
placing django_filter early enought in INSTALLED_APPS to thatit's
change_list.html is found before django's one.
Let me know if you are interested and in case I'm willing to share the code.
sandro
*:-)
-
-
Sandro Dentella *:-)
http://www.reteisi.org Soluzioni libere per le scuole
http://sqlkit.argolinux.org SQLkit home page - PyGTK/python/sqlalchemy