I use the key of another user, the sponsor, to indicate who is the sponsor of the User, and creates a link in the data store for those users who have a sponsor, and there can be no more than one, but the sponsor can sponsor many users like ID 2002 in this case that sponsored three other users:
In this case, this query does what I want: SELECT * FROM User where sponsor =KEY('agtzfmJuYW5vLXd3d3ILCxIEVXNlchjSDww')
, but I don't know how to program it with python, I can only use it in the data store. How can I execute a query by key when I want to map a set of users who have the same user as the key in the same field? A user in my model can have no more than one sponsor, and I just want to know who sponsored a particular person, which can be a list of users, and then they sponsored users, in turn, who I also want to request.
The field sponsor is the key, and it has a link to the sponsor in the data warehouse. I set the key in exactly the same way as user2.sponsor = user1.key and now I want to find everything that user1 sponsored with a request that should be like
User.All().filter('sponsor = ', user1.key)
but the sponsor is a key type field, so I donβt know how to match it to see, for example, a list of people whose active user is the sponsor and how it becomes a tree when the second generation also has links. How to choose a list of users for which this user is a sponsor, and then the second generation? When I modeled the relation simply as u1 = u2.key ie user2.sponsor = user1.key. Thanks for any hint.
The following workaround is bad practice, but my last and only resort:
def get(self): auser = self.auth.get_user_by_session() realuser = auth_models.User.get_by_id(long( auser['user_id'] )) q = auth_models.User.query() people = [] for p in q: try: if p.sponsor == realuser.key: people.append(p) except Exception, e: pass if auser: self.render_jinja('my_organization.html', people=people, user=realuser,)
Update
The problems are that a key property is not required and that Guido Van Rossum reported it as an error in ndb when I think it is an error in my code. This is what I am using now, which is a very acceptable solution, since every real user in the organization, with the possible exception of programmers, testers and administrators, must have a sponsor identifier, which is a user identifier.
from ndb import query class Myorg(NewBaseHandler): @user_required def get(self): user = auth_models.User.get_by_id(long(self.auth.get_user_by_session()['user_id'])) people = auth_models.User.query(auth_models.User.sponsor == user.key).fetch() self.render_jinja('my_organization.html', people=people, user=user) class User(model.Expando): """Stores user authentication credentials or authorization ids."""