简体   繁体   English

NDB,跨多个模型查询。 AppEngine上

[英]NDB, Querying across multiple models. AppEngine

I am struggling with querying across multiple models. 我正在努力跨多个模型进行查询。 This is what my class structure looks like: 这是我的类结构:

class User(ndb.Model):
    ...

class LogVisit(ndb.Model)
    user = ndb.KeyProperty(kind=User)
    ...

class LogOnline(ndb.Model)
    logVisit = ndb.KeyProperty(kind = LogVisit)
    ...

and I want to get a list of the user's LogOnline's 我想获得用户的LogOnline的列表

what I want to do is this: 我想做的是这样的:

qry = LogOnline.query(LogOnline.logvisit.get().user.get() == user)

However app engine wont allow me to use the get method within a query. 但是,应用程序引擎不允许我在查询中使用get方法。

Any thoughts on the best way to go about this? 有什么关于最佳解决方案的想法吗? Many thanks. 非常感谢。

The most efficient way will be to store the user's key in the LogOnline entity. 最有效的方法是将用户密钥存储在LogOnline实体中。 We can;t see the rest of your model to see what LogVisit adds to the whole excercise so difficult to see what LogVisit as an intermediate entity brings to the design. 我们看不到模型的其余部分,看不到LogVisit为整个练习增加了什么,因此很难看到LogVisit作为中间实体为设计带来了什么。

Then just 然后就

 LogOnline.query().filter(LogOnline.user == user)

You will have to stop thinking in terms of SQL if you want to have scalable applications on appengine. 如果要在appengine上具有可伸缩的应用程序,则必须停止考虑SQL。 Think in terms of pure entity relationships and don't try to normalize the data model. 考虑纯粹的实体关系,不要尝试规范化数据模型。 Intermediate entities like LogVisit tend to only be used if you need many to many relationships but are still inefficient if you have more than a few instances of them for a particular relationship. 诸如LogVisit之类的中间实体仅在需要多对多关系时才使用,但对于特定关系,如果它们有多个实例,则仍然效率不高。

You are doing it wrong. 你做错了。

# user variable is assumed to be a key
logonlines = [] # You can use set also
logvisits = LogVisit.query().filter(LogVisit.user == user).fetch()
for logvisit in logvisits:
    logOnlinesA = LogOnline.query().filter(LogOnline.logVisit == logvisit.key).fetch()
    logonlines.extend(logOnlinesA)

Give it a try : 试试看 :

logvisits = LogVisit.query().filter(LogVisit.user == user).fetch(keys_only=True)
logOnlinesA = LogOnline.query().filter(LogOnline.logVisit.in(logvisits)).fetch()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM