Question

I have the following model of Users and I want to get all the users that like 'yellow', but don't like 'red'.

class User(db.Model):
    name = db.StringProperty(required=True)
    favorite_colors = db.StringListProperty(required=True)

This works (all users that have at least one favorite color 'yellow' are returned):

results = db.GqlQuery(
    "SELECT * FROM User "
    "WHERE favorite_colors = 'yellow'")

But this does not do what I expected:

results = db.GqlQuery(
    "SELECT * FROM User "
    "WHERE favorite_colors = 'yellow' "
    "and favorite_colors != 'red'")

The same list of users is returned. I believe that it's testing if any of the favorite colors are different from 'red' and not if the list does not contain 'red' at all.

How can I filter only the results that contain an item and not another one?

Was it helpful?

Solution

You can't filter for the absence of an item. Your second query looks for everyone that has the item 'yellow' in their list, as well as at least one item that isn't 'red'.

If your set of items is limited, you may want to change your representation to include 'not' entries - eg, "yellow", "not blue", "not red". Otherwise, you'll need to do the filtering in Python code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top