Is there a negation filter available by default. The idea is that you can do the following in the django ORM:
model.objects.filter(field!=value)
How can I do that in tastypie if that is even possible. I tried:
someapi.com/resource/pk/?field__not=value
someapi.com/resource/pk/?field__!=value
someapi.com/resource/pk/?field!=value
And all of them given me errors.
Another way to do this without code changes is to use a iregex with inverse matching
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
I use exclude() to avoid some values. For example:
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):
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:
instead of the default:
should allow for the following syntax:
I haven't tested it. It could probably be written in more elegant way too, but should get you going :)
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 ...