I have a bunch of Widget objects.
Now each widget has a string property called ‘foo’. And I need to be able to query for Widgets that have ‘foo’ set to ‘red’, ‘orange’, ‘green’ or any arbitrary color, and include only Widgets that have that appropriate ‘foo’ property. Also, these colors are coming from user input, so I can’t trust them. I would rather not load all the results and then filter them, but rather do this in SQL.
However… I only see ways to join clauses with “AND”, never “OR”. And “in” is garbage (‘in’ only works on numerical IDs, or a sub-queryset, which reduces to the same problem!). I tried some other things, but they didn’t seem to work.
Basically, I’m asking how to express this as a key of filterQuery below.
myNiceWidgets = Widget.objects.filter(**filterQuery).orderBy(...)
Thanks.
To use
OR, look into django’sQobjects.https://docs.djangoproject.com/en/dev/topics/db/queries/#complex-lookups-with-q-objects
It won’t work as a key of
filterQueryas the Q object must be a positional argument.