views:

122

answers:

1

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?

A: 

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.

Nick Johnson
I don't know if I will be able to do the filtering in Python code because my set of "colors" has ~3k items and the "users" ~40k and the datastore API returns only the first 1000 results...Thanks anyway!
jbochi