Keyword query model

What I'm trying to do is request a datastore for a model where the key is not the key for the object that I already have. Here is the 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 does not have a partner who is not a user that we already know.

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

+4
source share
1 answer

There is a very simple way: get two entries and filter out your own user, if one is present.

 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 other = db.Query(User).filter('partner =', None).fetch(2) other = [u for u in other if u.key() != user.key()] if other: # connect user with other[0] else: # no one to connect to! 
+4
source

Source: https://habr.com/ru/post/1305186/


All Articles