App Engine中的db.ReferenceProperty()与ndb.KeyProperty [英] db.ReferenceProperty() vs ndb.KeyProperty in App Engine

查看:76
本文介绍了App Engine中的db.ReferenceProperty()与ndb.KeyProperty的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ReferenceProperty 在处理两个模块之间的引用时非常有用。例如:

  class UserProf(db.Model):
name = db.StringProperty(required = True)

class Team(db.Model):
manager_name = db.ReferenceProperty(UserProf,collection_name ='teams')
name = db.StringProperty(required = True)




  • 获取 manager_name 与团队实例,我们使用 team_ins.manager_name

  • 获得 teams which由特定的用户实例管理,我们使用 user_instance.teams 并迭代。



它看起来不容易理解吗?



在使用NDB做同样的事情时,我们必须修改

db.ReferenceProperty (UserProf,collection_name ='teams') - > ndb.KeyProperty(kind = UserProf)




  • team_ins.manager_name.get()会为您提供经理姓名

  • 为了让特定用户管理所有的团队,我们必须为团队中的Team.query(Team.manager_name)完成

      == user_ins.key):
    printteam name:,team.name


>

正如您所看到的,处理这些类型的方案在db中比在ndb中更容易和易读。


  • 在ndb中删除ReferenceProperty的原因是什么?
  • 甚至db的查询 user_instance.teams 也会和ndb的for循环做同样的事情。但在ndb中,我们明确提到了使用for循环。
  • 当我们执行 user_instance.teams



感谢您提前致谢。

解决方案

<蒂姆解释得很好。我们发现一个常见的反模式是使用引用属性并一次加载它们,因为符号entity.property1.property2没有说明第一个点导致数据库get操作。所以我们通过强制你编写entity.property1.get()。property2来使它更加明显,我们通过简单地说entity.property1.get_async使批量预取变得更容易(没有Nick博客的复杂解决方案) ()为一组实体 - 这排队一个批处理获取操作没有阻塞的结果,当你下一次引用任何这些属性使用entity.property1.get()。property2这不会启动另一个获得操作,但只是等待该批次完成(第二次执行此操作时,批次获取已完成)。这种进行中和memcache集成也是免费的。


ReferenceProperty was very helpful in handling references between two modules. For example:

class UserProf(db.Model):
    name = db.StringProperty(required=True)

class Team(db.Model):
    manager_name = db.ReferenceProperty(UserProf, collection_name='teams')
    name = db.StringProperty(required=True)

  • To get manager_name with team instance, we use team_ins.manager_name.
  • To get teams which are managed by particular user instance, we use user_instance.teams and iterate over.

Doesn't it look easy and understandable?

In doing same thing using NDB, we have to modify

db.ReferenceProperty(UserProf, collection_name='teams') --> ndb.KeyProperty(kind=UserProf)

  • team_ins.manager_name.get() would give you manager name
  • To get all team which are manger by particular user, we have to do

    for team in Team.query(Team.manager_name == user_ins.key):         
        print "team  name:", team.name
    

As you can see handling these kind of scenarios looks easier and readable in db than ndb.

  • What is the reason for removing ReferenceProperty in ndb?
  • Even db's query user_instance.teams would have doing the same thing as it is done in ndb's for loop. But in ndb, we are explicitly mentioning using for loop.
  • What is happening behind the scenes when we do user_instance.teams?

Thanks in advance..

解决方案

Tim explained it well. We found that a common anti-pattern was using reference properties and loading them one at a time, because the notation "entity.property1.property2" doesn't make it clear that the first dot causes a database "get" operation. So we made it more obvious by forcing you to write "entity.property1.get().property2", and we made it easier to do batch prefetching (without the complex solution from Nick's blog) by simply saying "entity.property1.get_async()" for a bunch of entities -- this queues a single batch get operation without blocking for the result, and when you next reference any of these properties using "entity.property1.get().property2" this won't start another get operation but just waits for that batch get to complete (and the second time you do this, the batch get is already complete). Also this way in-process and memcache integration comes for free.

这篇关于App Engine中的db.ReferenceProperty()与ndb.KeyProperty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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