Re: django admin integration?

131 views
Skip to first unread message

Florian Apolloner

unread,
Mar 25, 2013, 12:48:08 PM3/25/13
to django...@googlegroups.com
No real plan yet and I don't know of any efforts either.

Regards,
Florian

Steve

unread,
Mar 25, 2013, 12:50:38 PM3/25/13
to django...@googlegroups.com

List filters are now pluggable, deprecating large swaths of this project, at least as far as admin integration is concerned

--
You received this message because you are subscribed to the Google Groups "django-filter" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-filte...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Alessandro Dentella

unread,
Mar 26, 2013, 2:21:59 PM3/26/13
to django...@googlegroups.com
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



David Brailovsky

unread,
Mar 27, 2013, 5:47:09 PM3/27/13
to django...@googlegroups.com, san...@e-den.it
This sounds exactly like what I imagined.
I would love to use the code if you could share it somehow.
I'm also willing to put some effort to cleanup the code / add tests to merge it into django-filters

Alessandro Dentella

unread,
Mar 31, 2013, 12:38:10 PM3/31/13
to django...@googlegroups.com
On Wed, Mar 27, 2013 at 02:47:09PM -0700, David Brailovsky wrote:
> This sounds exactly like what I imagined.
> I would love to use the code if you could share it somehow.

Ok, I'll do that shortly, possibly tomorrow

> I'm also willing to put some effort to cleanup the code / add tests to
> merge it into django-filters

That'd be great.

sandro
*:-)

Alessandro Dentella

unread,
Apr 10, 2013, 2:02:53 AM4/10/13
to django...@googlegroups.com
I'm sorry it took me much more time to get it in a suitable shape.
I created a mercurial repo at

http://hg.argolinux.org/py/django-filter-sd/

It also contains a working test project, so you can get an idea.

It's a fork of a release of django-filter around end of february, with the
following patches:

1. DateRange: adds last year/last month
2. Choicefilter: addes '--------' for no choce
3. ability to set lookup_type from field_name (eg.: filrst_name__icontains)
4. settings a MultipleChoice when __in lookup type is used
5. settings DateRange when __range is used
6. choices from model if no choice are present (maybe not usefull...)
7. added a template loader that implements { extends
'admin:change_list.html' } for a cleaner and less instrusive/
Was snippet http://djangosnippets.org/snippets/1376/
8. added support for admin integration (templatetag, templates, js....)
9. complete test project

As I used buildout included are instructions for setup the virtualenv with
buildout that boils down to:

hg clone http://hg.argolinux.org/py/django-filter-sd
cd django-filter-sd
python bootstrap -d
bin/buildout (takes some time)
bin/django runserver

I'd be please to have some feedback and possibly some patches to it could be
considered for a django-filter integration.


sandro
*:-)




--
Reply all
Reply to author
Forward
0 new messages