本文介绍了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



感谢您提前致谢。

解决方案


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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 11:40