28👍
Unfortunately there’s not.
The problem is that Tastypie’s ModelResource class uses the filter() method of the QuerySet only, i.e. it does not use exclude() which should be used for negative filters. There is no filter() field lookup that would mean negation though. The valid lookups are (after this SO post):
exact
iexact
contains
icontains
in
gt
gte
lt
lte
startswith
istartswith
endswith
iendswith
range
year
month
day
week_day
isnull
search
regex
iregex
However it shouldn’t be so hard to implement the support for something like “__not_eq”. All you need to do is to modify the apply_filters() method and separate filters with “__not_eq” from the rest. Then you should pass the first group to exclude() and the rest to filter().
Something like:
def apply_filters(self, request, applicable_filters):
"""
An ORM-specific implementation of ``apply_filters``.
The default simply applies the ``applicable_filters`` as ``**kwargs``,
but should make it possible to do more advanced things.
"""
positive_filters = {}
negative_filters = {}
for lookup in applicable_filters.keys():
if lookup.endswith( '__not_eq' ):
negative_filters[ lookup ] = applicable_filters[ lookup ]
else:
positive_filters[ lookup ] = applicable_filters[ lookup ]
return self.get_object_list(request).filter(**positive_filters).exclude(**negative_filters)
instead of the default:
def apply_filters(self, request, applicable_filters):
"""
An ORM-specific implementation of ``apply_filters``.
The default simply applies the ``applicable_filters`` as ``**kwargs``,
but should make it possible to do more advanced things.
"""
return self.get_object_list(request).filter(**applicable_filters)
should allow for the following syntax:
someapi.com/resource/pk/?field__not_eq=value
I haven’t tested it. It could probably be written in more elegant way too, but should get you going 🙂
6👍
Another way to do this without code changes is to use a iregex with inverse matching
http://HOST/api/v1/resource/?format=json&thing__iregex=^((?!notThis).)*$
- Use of unicode in Django
- Celery workers unable to connect to redis on docker instances
- Can Django run on Gunicorn alone (no Apache or nginx)?
- How do I convert kilometres to degrees in Geodjango/GEOS?
2👍
I’ve opened a bug for this and provided a simple solution here: https://github.com/toastdriven/django-tastypie/issues/524
It would probably be nicer to add the ‘!’ character at the end of the field name as you’ve done in your question …
- Installing django 1.5(development version) in virtualenv
- How to make Django Password Reset Email Beautiful HTML?
- Managing multiple settings.py files
1👍
Note on Gorneau’s above answer: seems like this only works if you’re not using a MySQL backend. See:
#1139 – Got error 'repetition-operator operand invalid' from regexp
- Django is very slow on my machine
- Creating Partial Indexes with Django 1.7
- Where do I import the `DoesNotExist` exception in Django 1.10 from?
- Django's get_current_language always returns "en"
- Can Django run on Gunicorn alone (no Apache or nginx)?
-1👍
I use exclude() to avoid some values. For example:
Person.filter(name="Tim").exclude(state="Down");
- Uwsgi segmentation fault when serving a django application
- Apache strips down "Authorization" header
- Changing password in Django Admin
- Django global variable
- Django logging – django.request logger and extra context