不幸的是没有。
问题是 Tastypie 的 ModelResource 类仅使用 QuerySet 的 filter() 方法,即它不使用 exclude() 应该用于否定过滤器的方法。没有过滤器()字段查找意味着否定。有效的查找是(在此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
然而,实现对“__not_eq”之类的支持应该不难。您需要做的就是修改 apply_filters() 方法并使用“__not_eq”将过滤器与其余过滤器分开。然后你应该将第一组传递给 exclude(),其余的传递给 filter()。
就像是:
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)
而不是默认值:
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)
应该允许以下语法:
someapi.com/resource/pk/?field__not_eq=value
我没有测试过。它可能也可以用更优雅的方式编写,但应该让你继续前进:)