1

One of my fields in my model has a choice field:

STATUS = Choices(
   ('first', _('car')),
   ('second', _('motorcycle')),
   ('third', _('bicycle')),
)

My filter function looks like this:

choice = request.GET.get('choice')

vehicles = vehicles.filter(status=choice).order_by('-date_posted')

The filter works, when I select only one choice, but when I am trying to select more than one choice it only catches the last one selected.

The query looks like that:

?choice=first&choice=second

Any idea how to make it, so it would display items, based on more than one choice?

1 Answer 1

2

You can use .getlist(…) [Django-doc] to obtain the list of options, and then use the __in lookup [Django-doc] to retrieve the vehicles where the status is one of the elements in the list:

choices = request.GET.getlist('choice')

vehicles = vehicles.filter(status__in=choices).order_by('-date_posted')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.