Incomplete implementation of Odata.
I'll take pull requests, though!
import odata
def foo(request):
odata_filtertext = request.GET.get('$filter')
if odata_filtertext:
model_query = odata.django_filter(odata_filtertext)
MyModel.objects.filter(model_query)
...
You'll notice if you're looking for something comprehensive, this isn't it. Odata has some CRAZY functions a bunch of which don't really map well to the Django ORM anyway. However, if you're doing 'normal'ish queries, then a lot is supported.
-
$filter=
- field name lookup
- Logical Operators (
and
,or
,not
) and (eq
,ne
,gt
,ge
,lt
,le
) - Date support
-
contains(name, val)
function -
has
operator - arithmetic operators (addition, subtraction, etc)
- other expression functions (
indexof
,startswith
,endswith
) - attribute functions (
length
,concat
,trim
,day
,now
, etc) -
@
variable references
-
$orderby=
start_date gt '2017-03-01'
start_date ge '2017-03-01' and start_date lt '2017-03-02'
location/postal_code eq '22980'
contains(name, 'Sessions') or contains(name, 'DeVos')
location/postal_code eq '22980' and (contains(name, 'Sessions') or contains(name, 'DeVos'))
(contains(name, 'Sessions') or contains(name, 'DeVos')) and location/postal_code eq '22980'
Check out the code to see what you can do, but some other features include:
- Customize your field mapping (e.g. map
location/addr
to something other thanlocation__addr
) - Subclass/replace
odata.DjangoFilterAdapter
and pass that toodata.FilterProcessor
if you want to use this code to support, e.g. SQLAlchemy or some other orm or query builder.