alex/django-filter — pull request #162 by eosrei (2014-09-12 21:44:37+00:00)
Title: New Feature - Simplified field lookup type filters including tests and docs

RDF/django-filter currently supports filter_fields for exact filtering only:
```
class ProductList(generics.ListAPIView):
queryset = Product.objects.all()
serializer_class = ProductSerializer
filter_fields = ('category', 'in_stock')
```

Creating many custom lookup filters can be tedious with DRF and django-filter:

class SessionFilter(django_filters.FilterSet):
client_type__contains = django_filters.CharFilter(name='client_type', lookup_type='contains')
client_system__contains = django_filters.CharFilter(name='client_system', lookup_type='contains')

starttime__gte = django_filters.DateTimeFilter(name='starttime', lookup_type='gte')
starttime__lte = django_filters.DateTimeFilter(name='starttime', lookup_type='lte')
class Meta:
model = Session
fields = ['client_type',
'client_system',
'starttime',
'project',
'language',
]

class SessionList(generics.ListAPIView):
queryset = Session.objects.all()
serializer_class = SessionSerializer
filter_class = SessionFilter

Here's the same thing with TastyPie:

class SessionResource(ModelResource):
class Meta:
queryset = Session.objects.all()
resource_name = "session"
allowed_methods = ['get']
serializer = JSONSerializer()
filtering = {
"client_type": ['exact', 'contains',],
"client_system": ['exact', 'contains'],
"starttime": ['exact', 'gte', 'lte'],
"project": ['exact'],
"language": ['exact'],
}

I've implemented support for a fields Dictionary for django-filter. This Pull request includes tests, docs, and is backwards compatible for existing implementations.

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.

This feature request was first discussed on the [DRF issue queue](https://github.com/tomchristie/django-rest-framework/issues/1856), but I realized it could be entirely implemented in django-filter.