Oleg Tarasenko
Oleg Tarasenko

Reputation: 9610

django: filtering on undefined number of options

I am trying to process undefined number of filtering options given to a views (in a post request).

For example if we have a view:

def get_posts_by_category(request): print request.POST

if request.POST:
    categories = [Categories.objects\
                  .get(cat=item) for item in request.POST["category"].split(",")[:-1]]

so view takes a list of categories, which can consist from 1 - n items.

Could someone suggest how can I get all articles, with categories I've got from POST?

Upvotes: 1

Views: 108

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599610

Article.objects.filter(category__in=request.POST.getlist('category'))

Upvotes: 1

Related Questions