i using multiselectfield select multiple choices within django admin creates array fields in backend of choices select. use django tastypie's
list field make sure list of elements api returns.
my problem building filter when put /api/?brand_category=clothing&q=athletic,bohemian
in browser not return empty list. want know if doing wrong? or not building filters correctly?
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 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 response
{ "brand_category": [ "clothing" ], "brand_style": [ "athletic", "bohemian", "casual" ] }
filters['q']
athletic,bohemian
string. __in
lookup need list or tuple.
query = filters['q'].split(',')
Comments
Post a Comment