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.
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
You can do this using field1__in=['x', 'y']
Samplemodel.objects.filter(field1__in=['x', 'y'])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With