- Notifications
You must be signed in to change notification settings - Fork77
Django URL Filter provides a safe way to filter data via human-friendly URLs.
License
miki725/django-url-filter
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Django URL Filter provides a safe way to filter data via human-friendly URLs.
- Free software: MIT license
- GitHub:https://github.com/miki725/django-url-filter
- Documentation:http://django-url-filter.readthedocs.io/
The main goal of Django URL Filter is to provide an easy URL interfacefor filtering data. It allows the user to safely filter by modelattributes and also allows to specify the lookup type for each filter(very much like Django's filtering system in ORM).
For example the following will retrieve all items where the id is5 and title contains"foo":
example.com/listview/?id=5&title__contains=foo
In addition to basic lookup types, Django URL Filter allows touse more sophisticated lookups such asin oryear.For example:
example.com/listview/?id__in=1,2,3&created__year=2013
- Python 2.7, 3.x, pypy or pypy3
- Django 1.8+ (there are plans to support older Django versions)
- Django REST Framework 2 or 3 (only if you want to use DRF integration)
Easiest way to install this library is by usingpip:
$ pip install django-url-filter
To make example short, it demonstrates Django URL Filter integrationwith Django REST Framework but it can be used without DRF (see below).
from url_filter.integrations.drf import DjangoFilterBackendclass UserViewSet(ModelViewSet): queryset = User.objects.all() serializer_class = UserSerializer filter_backends = [DjangoFilterBackend] filter_fields = ['username', 'email']
Alternatively filterset can be manually created and used directlyto filter querysets:
from django.http import QueryDictfrom url_filter.filtersets import ModelFilterSetclass UserFilterSet(ModelFilterSet): class Meta(object): model = Userquery = QueryDict('email__contains=gmail&joined__gt=2015-01-01')fs = UserFilterSet(data=query, queryset=User.objects.all())filtered_users = fs.filter()Above will automatically allow the use of all of the Django URL Filter features.Some possibilities:
# get user with id 5example.com/users/?id=5# get user with id either 5, 10 or 15example.com/users/?id__in=5,10,15# get user with id between 5 and 10example.com/users/?id__range=5,10# get user with username "foo"example.com/users/?username=foo# get user with username containing case insensitive "foo"example.com/users/?username__icontains=foo# get user where username does NOT contain "foo"example.com/users/?username__icontains!=foo# get user who joined in 2015 as per user profileexample.com/users/?profile__joined__year=2015# get user who joined in between 2010 and 2015 as per user profileexample.com/users/?profile__joined__range=2010-01-01,2015-12-31# get user who joined in after 2010 as per user profileexample.com/users/?profile__joined__gt=2010-01-01
Human-friendly URLs
Filter querystring format looksvery similar to syntax for filtering in Django ORM.Even negated filters are supported! Some examples:
example.com/users/?email__contains=gmail&joined__gt=2015-01-01example.com/users/?email__contains!=gmail # note !
Related models
Support related fields so that filtering can be applied to relatedmodels. For example:
example.com/users/?profile__nickname=foo
Decoupled filtering
How URLs are parsed and how data is filtered is decoupled.This allows the actual filtering logic to be decoupled from Djangohence filtering is possible not only with Django ORM QuerySet butany set of data can be filtered (e.g. SQLAlchemy query objects)assuming corresponding filtering backend is implemented.
Usage-agnostic
This library decouples filtering from any particular usage-pattern.It implements all the basic building blocks for creatingfiltersets but it does not assume how they will be used.To make the library easy to use, it ships with some integrationswith common usage patterns like integration with Django REST Framework.This means that its easy to use in custom applications with customrequirements (which is probably most of the time!)
About
Django URL Filter provides a safe way to filter data via human-friendly URLs.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors11
Uh oh!
There was an error while loading.Please reload this page.