2012-07-27 5 views
5

가능한 중복 :Tastypie에서 하나 개의 쿼리에 필터를 결합

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'], 
    } 

은 내가이 필요합니다 :


Django Tastypie Advanced Filtering: How to do complex lookups with Q objects

나는이처럼 보이는 tastypie modelRseource이 OR 연산자를 사용하면 쿼리를 하나의 매개 변수로 결합하는 것이 좋습니다. 예를 들어, "test"라는 단어가 들어있는 객체를 주석 필드 또는 media_text 필드에서 반환하려고합니다.

이 이상적 일 것이다 : HTTP : mysite.com/api/v1/tagged Q = 테스트 '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 

답변

1

나는 '그것이 드리겠습니다 확신하지 비록 Advanced Filtering보고 제안 . 그러나 build_filters를 덮어 쓰는 경우 전체 리소스에 액세스 할 수 있으므로 가능한 것보다 많은 필드에 걸쳐있는 필터를 정의 할 수 있습니다. 당신은 build_filters를 재정 의하여 옳은 일을하고있는

+0

그래, 내가 고급 필터링을 조사하고 있었고 실제로 OR 문을 수행하는 방법을 알지 못했습니다. 내가 가지고있는 것은 AND 조건으로 필터링하는 것입니다. – bevinlorenzo

+0

원본 질문에 고급 필터를 추가했습니다. – bevinlorenzo

+0

내가 말한 것은 당신이 실제로 자신의 필터를 정의해야한다는 것입니다. 그러면 이것은 tastypie 필터링에 추가 할 수있는 장소입니다. –

관련 문제