我有一个美味的模型资源,看起来像这样:
class TaggedResource(ModelResource):
tags = ListField()
user = fields.ForeignKey(UserProfileResource, 'user')
class Meta:
queryset = Media.objects.all().order_by('-timestamp')
authorization = MediaAuthorization()
detail_allowed_methods = ['get', 'post', 'put', 'delete','patch']
filtering = {
#'user': ALL_WITH_RELATIONS,
#exact is date, lt is less than lte less than equal to, etc
'timestamp': ['exact', 'range', 'lt', 'lte', 'gte', 'gt'],
'social_source': ALL,
'media_type': ALL,
'comment': ['exact', 'startswith', 'endswith', 'contains'],
'media_text': ['exact', 'startswith', 'endswith', 'contains'],
}
我需要在过滤器之间有一个 OR 运算符,并且希望将查询组合成一个参数。例如,我想返回包含从评论字段或 media_text 字段过滤的单词“test”的对象。
这将是理想的:http:mysite.com/api/v1/tagged?q=test
其中 'q' 对两个字段执行 OR 过滤器。
这是可行的吗?
更新:这是我正在使用高级过滤器进行的工作,但我不确定如何获得 OR 语句:
def build_filters(self, filters=None):
if filters is None:
filters = {}
orm_filters = super(TaggedResource, self).build_filters(filters)
if 'q' in filters:
orm_filters['comment__contains'] = filters['q']
orm_filters['media_text__contains'] = filters['q']
return orm_filters