我使用MultiSelectField在我的django管理中选择多个选项它为我选择的所有选项的后端字段创建了一个数组。然后我使用django tastypie's
List字段来确保它是api返回的元素列表。你知道吗
我的问题是,当我在构建过滤器时,我将/api/?brand_category=Clothing&q=athletic,bohemian
放到浏览器中,它只返回一个空列表。所以我想知道我是不是做错了什么?或者没有正确构建过滤器?你知道吗
型号.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"
]
}
filters['q']
是athletic,bohemian
字符串。__in
查找需要一个列表或元组。你知道吗相关问题 更多 >
编程相关推荐