5

I have a model (1) that has a field pointing to another model (2). I have a list of values coming from model (2) and I would like to filter objects in model(1) on all of them.

Basically I want to do this:

SomeModel.objects.filter(field1=x OR field1=y OR field1=z)

Is this possible, can't find anything in docs.

2 Answers 2

12

If you want to 'OR' them together, Q objects should help you achieve this:

from django.db.models import Q
Samplemodel.objects.filter(Q(field1='x') | Q(field1='y'))

Ref link: http://docs.djangoproject.com/en/dev/topics/db/queries/#complex-lookups-with-q-objects

Sign up to request clarification or add additional context in comments.

Comments

3

You can do this using field1__in=['x', 'y']

Samplemodel.objects.filter(field1__in=['x', 'y'])

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.