How is it possible to run the following:
someQuerySet.filter(keyword='someKey')
someQuerySet.filter(keyword='someOtherKey')
I get InvalidQueryError: Duplicate query conditions whenever I try to do that. I know it's possible to filter by a list of values, but right now, I need to do individual filters.
Later edit: I'm actually using:
someQuerySet.filter(keyword__ne='someKey')
someQuerySet.filter(keyword__ne='someOtherKey')
You could build up Q objects like this:
from django.db.models import Q
filters = Q(keyword='someKey')
…
filters = filters | Q(keyword='someOtherKey')
someQuerySet.filter(filters)
This will basically create a WHERE clause like this: WHERE keyword = 'someKey' OR keyword = 'someOtherKey'
I am doing this from memory, so let me know if this doesn't work, and I will look into some of my past code.
The reason for the error is because by default the queries parameters AND. So you are asking for keyword="SomeKey" AND keyword="SomeOtherKey" which can never be true.
You could use Q objects to or like: http://docs.mongoengine.org/en/latest/guide/querying.html#advanced-queries or do a $in where the value matches any in a list eg: keyword__in=["SomeKey", "SomeOtherKey"]
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