2016-09-01 6 views
0

MultiSelectField私はdjango管理者の中で複数の選択肢を選択するために、選択したすべての選択肢のバックエンドのフィールドの配列を作成します。次に、django tastypie'sリストフィールドを使用して、APIが返す要素のリストを確認します。django tastypieを使用した要素リストの事前フィルタリング

私の問題は、/api/?brand_category=Clothing&q=athletic,bohemianをブラウザに入れたときにフィルタを構築しているときに、空のリスト以外は何も返しません。だから私は何か間違っているかどうかを知りたいのですか?または、フィルタを正しく構築しないでください。

models.py

class Brand(models.Model): 

    # category 
    brand_category = MultiSelectField(max_length=100, blank=True, choices=categories)) 

    # style 
    brand_style = MultiSelectField(max_length=100, choices=styles, blank=True) 

api.py

class LabelResource(ModelResource): 

    brand_category = fields.ListField(attribute='brand_category') 

    brand_style = fields.ListField(attribute='brand_style') 

    class Meta: 

     filtering = { 
      "brand_category": ALL, 
      "brand_style": ALL, 
      "q": ['exact', 'startswith', 'endswith', 'contains', 'in'], 

     } 


def build_filters(self, filters=None): 
    if filters is None: 
     filters = {} 

    orm_filters = super(LabelResource, self).build_filters(filters) 


    if('q' in filters): 
     query = filters['q'] 

     qset = (
      Q(brand_style__in=query) 
     ) 

     orm_filters.update({'custom': qset}) 

    return orm_filters 

def apply_filters(self, request, applicable_filters): 
    if 'custom' in applicable_filters: 
     custom = applicable_filters.pop('custom') 

    else: 
     custom = None 

    semi_filtered = super(LabelResource, self).apply_filters(request, applicable_filters) 

    return semi_filtered.filter(custom) if custom else semi_filtered 

JSONレスポンス

{ 
    "brand_category": [ 
    "Clothing" 
    ], 
    "brand_style": [ 
    "athletic", 
    "bohemian", 
    "casual" 
    ] 
} 

答えて

0

filters['q']athletic,bohemian文字列です。 __inルックアップにはリストまたはタプルが必要です。

query = filters['q'].split(',') 
関連する問題