Skip to content

Commit

Permalink
Adding FieldSet Meta fields Dictionary docs
Browse files Browse the repository at this point in the history
  • Loading branch information
13rac1 committed Sep 12, 2014
1 parent 9dd4a1b commit a0a4873
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ Vladimir Sidorenko
Tom Christie
Remco Wendt
Axel Haustant
Brad Erickson
15 changes: 15 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ And then in your view you could do::
filter = ProductFilter(request.GET, queryset=Product.objects.all())
return render_to_response('my_app/template.html', {'filter': filter})

Django-filters additionally supports specifying FilterSet fields using a
dictionary to specify filters with lookup types::
import django_filters

class ProductFilter(django_filters.FilterSet):
class Meta:
model = Product
fields = {'name': ['exact', 'icontains'],
'price': ['exact', 'gte', 'lte'],
}

The filters will be available as 'name', 'name__icontains', 'price',
'price__gte', and 'price__lte' in the above example.

Support
-------

Expand Down
18 changes: 17 additions & 1 deletion docs/usage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,22 @@ and wonder why search for "foo" doesn't return result for "foobar". It's because
default lookup type is exact text, but you probably want `icontains` lookup
field.**

The FilterSet Meta class fields can additionally be set using a Dictionary to
specify multiple ``lookup_type`` filters without significant code duplication::

import django_filters

class ProductFilter(django_filters.FilterSet):
class Meta:
model = Product
fields = {'price': ['lt', 'gt'],
'release_date', ['exact'],
}

The above would generate 'price__lt', 'price__gt' and 'release_date' filters.
The filter lookup type keyword 'exact' is the default and therefore never added
to a filter name.

Items in the ``fields`` sequence in the ``Meta`` class may include
"relationship paths" using Django's ``__`` syntax to filter on fields on a
related model::
Expand All @@ -78,7 +94,7 @@ in ``Filter.extra``, so it's possible to override the initializer of a
self.filters['manufacturer'].extra.update(
{'empty_label': 'All Manufacturers'})

Like ``django.contrib.admin.ModelAdmin`` does it is possible to override
Like ``django.contrib.admin.ModelAdmin``, it is possible to override
default filters for all the models fields of the same kind using
``filter_overrides``::

Expand Down

0 comments on commit a0a4873

Please sign in to comment.