如何创建匹配键的查询? [英] How to create a query for matching keys?

查看:106
本文介绍了如何创建匹配键的查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用另一个用户(发起人)的密钥来指示谁是用户的发起人,并且它为具有发起人的用户在数据存储中创建链接,并且它可以是至多一个,但发起人可以赞助许多用户喜欢在这种情况下ID 2002谁赞助三个其他用户:

SELECT * FROM用户其中sponsor = KEY('')但我不知道如何使用python编程,我只能用它来访问数据存储。当我想要匹配在同一字段中具有与用户相同的用户密钥的用户组时,如何通过密钥进行查询?我的模型中的用户最多只能拥有一个赞助商,我只想知道某个特定人员赞助了哪些可能是用户列表,然后轮到他们赞助用户,我也想查询。

字段赞助商是一个关键字,它与数据存储中的发起人有一个链接。我设置密钥就像user2.sponsor = user1.key,现在我想找到所有user1赞助的查询,应该就像



User.All()。filter('sponsor =',user1.key)



但赞助商是一个键入字段的类型我不知道如何匹配它,以查看例如活跃用户是赞助商的人的列表以及当第二代人也具有链接时它如何变成树。如何选择此用户是赞助商的用户列表,然后是第二代?当我建模关系就像u1 = u2.key即user2.sponsor = user1.key。感谢您的任何提示



以下解决方法是不好的做法,但是我最后也是唯一的解决方案:

  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)
除了例外,e:
如果使用者传递

self.render_jinja('my_organization.html',people = people,user = realuser, )



更新



问题是关键属性并不是必需的,Guido Van Rossum在ndb中报告这是一个错误,当我认为它是我的代码中的一个错误时。这是我现在使用的,这是一个非常可接受的解决方案,因为组织中的每个真实用户除了可能程序员,测试人员和管理员都需要拥有一个用户ID的赞助商ID。

  from ndb导入查询
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):
存储用户认证凭证或授权标识。

#:用于确保唯一性的模型。
unique_model =唯一
#:用于存储令牌的模型。
token_model = UserToken
sponsor = KeyProperty()
created = model.DateTimeProperty(auto_now_add = True)
updated = model.DateTimeProperty(auto_now = True)
#ID用于第三方认证,例如谷歌:用户名。独特。
auth_ids = model.StringProperty(重复= True)
#哈希密码。不需要,因为第三方认证
#不使用密码。
password = model.StringProperty()
...


解决方案

.google.com / document / d / 1dsx1hihmMXMJm8wIRu49tJR-KEng80o3wkg4Nlbqn-w / edit?hl = zh_CN#heading = h.j07awmd5m00o> docs


另一个有用的技巧是查询Expando类型的动态
属性。您将无法使用class.query(class.propname ==
值),因为该类没有属性对象。相反,您可以
使用ndb.query.FilterNode类构造过滤器表达式
,如下所示:



< pre $ 从ndb导入模型,查询

class X(model.Expando):
@classmethod
def query_for(cls,name,值):
return cls.query(query.FilterNode(name,'=',value))

print X.query_for('blah',42).fetch()

请尝试:

  form ndb导入查询

def get(self):
auser = self.auth.get_user_by_session()
realuser = auth_models.User.get_by_id(long(auser ['user_id']))
people = auth_models.User.query(query.FilterNode('sponsor','=',realuser.key))。fetch()$ b $如果使用者:
self.render_jinja('my_organization.html',people = people,user = realuser,)


I use the key of another User, the sponsor, to indicate who is the sponsor of a User and it creates a link in the datastore for those Users that have a sponsor and it can be at most one but a sponsor can sponsor many users like in this case ID 2002 who 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 that with python, I can only use it to the datastore. How can I query by key when I want to match the set of users who has the same user as key in the same field? A user in my model can have at most one sponsor and I just want to know who a particular person sponsored which could be a list of users and then they sponsored users in their turn which I also want to query on.

The field sponsor is a key and it has a link to the sponsor in the datastore. I set the key just like user2.sponsor = user1.key and now I want to find all that user1 sponsored with a query that should be just like

User.All().filter('sponsor = ', user1.key)

but sponsor is a field of type key so I don't know how to match it to see for example a list a people the active user is a sponsor for and how it becomes a tree when the second generation also have links. How to select the list of users this user is a sponsor for and then the second generation? When i modelled the relation simply like u1=u2.key ie user2.sponsor=user1.key. Thanks for any hint

The following workaround is bad practice but is 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 issues are that the keyproperty is not required and that Guido Van Rossum has reported this as a bug in the ndb when I think it's a bug in my code. Here's what I'm using now, which is a very acceptable solution since every real user in the organization except possibly programmers, testers and admins are going the be required to have a sponsor ID which is a user ID.

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."""

    #: The model used to ensure uniqueness.
    unique_model = Unique
    #: The model used to store tokens.
    token_model = UserToken
    sponsor = KeyProperty()
    created = model.DateTimeProperty(auto_now_add=True)
    updated = model.DateTimeProperty(auto_now=True)
    # ID for third party authentication, e.g. 'google:username'. UNIQUE.
    auth_ids = model.StringProperty(repeated=True)
    # Hashed password. Not required because third party authentication
    # doesn't use password.
    password = model.StringProperty()
    ...

解决方案

The User model is an NDB Expando which is a little bit tricky to query.

From the docs

Another useful trick is querying an Expando kind for a dynamic property. You won't be able to use class.query(class.propname == value) as the class doesn't have a property object. Instead, you can use the ndb.query.FilterNode class to construct a filter expression, as follows:

from ndb import model, query

class X(model.Expando):
  @classmethod
  def query_for(cls, name, value):
    return cls.query(query.FilterNode(name, '=', value))

print X.query_for('blah', 42).fetch()

So try:

form ndb import query

def get(self):
    auser = self.auth.get_user_by_session()
    realuser = auth_models.User.get_by_id(long( auser['user_id'] ))
    people = auth_models.User.query(query.FilterNode('sponsor', '=', realuser.key)).fetch()
    if auser: 
        self.render_jinja('my_organization.html', people=people, user=realuser,)

这篇关于如何创建匹配键的查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆