thể trùng lặp:
Django Tastypie Advanced Filtering: How to do complex lookups with Q objectsKết hợp bộ lọc vào một truy vấn trong Tastypie
Tôi có một modelRseource tastypie trông như thế này:
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'],
}
tôi cần phải có một HOẶC vận hành giữa các bộ lọc và rất thích kết hợp truy vấn vào một tham số. Ví dụ, tôi muốn trả về các đối tượng có chứa từ "kiểm tra" lọc từ trường bình luận HOẶC trường phương tiện.
Đây sẽ là lý tưởng: http: mysite.com/api/v1/tagged q = test
nơi 'q' thực hiện một HOẶC lọc cho cả hai lĩnh vực.
Điều này có thể thực hiện được không?
UPDATE: Dưới đây là những gì tôi đang làm việc trên với các bộ lọc tiên tiến nhưng không thực sự chắc chắn làm thế nào để có được một tuyên bố 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
Đây là những gì tôi cần. Cảm ơn! – bevinlorenzo