Query for model by key

Posted by Jason Hall on Stack Overflow See other posts from Stack Overflow or by Jason Hall
Published on 2010-03-26T03:39:29Z Indexed on 2010/03/26 3:43 UTC
Read the original article Hit count: 248

What I'm trying to do is query the datastore for a model where the key is not the key of an object I already have. Here's some code:

class User(db.Model):
partner = db.SelfReferenceProperty()

def text_message(self, msg):
    user = User.get_or_insert(msg.sender)

    if not user.partner:
        # user doesn't have a partner, find them one
        # BUG: this line returns 'user' himself... :(
        other = db.Query(User).filter('partner =', None).get()
        if other:
            # connect users
        else:
            # no one to connect to!

The idea is to find another User who doesn't have a partner, that isn't the User we already know.

I've tried filter('key !=, user.key()), filter('__key__ !=, user.key()) and a couple others, and nothing returns another User who doesn't have a partner. filter('foo !=, user.key()) also returns nothing, for the record.

© Stack Overflow or respective owner

Related posts about google-app-engine

Related posts about python