使用Google App Engine从db.ReferenceProperty切换到NDB?

纸袋作家

今天,我在这里阅读了有关如何建立在GAE REST API中使用的一对多关系的文章。

https://cloud.google.com/appengine/articles/modeling

我以为这很简单,但是后来我意识到我正在使用ndb而不是db,所以我不能使用db.ReferenceProperty。

我在这里有一个模型路线

class Route(ndb.Model):

  driver_id = ndb.IntegerProperty()
  requester_id = ndb.IntegerProperty()
  startpoint = ndb.GeoPtProperty(required=True)
  endpoint = ndb.GeoPtProperty(required=True)
  regular = ndb.BooleanProperty(required=True)
  date_and_time = ndb.DateTimeProperty(required=True)
  places_available = ndb.IntegerProperty()
  val_lift = ndb.IntegerProperty()

这是我的模型RegularDays,如果接收到的JSON中的Regular为True时将使用该模型

class RegularDays(ndb.Model):

  route = db.ReferenceProperty(Route,
                                   collection_name='regular_days')
  Monday = ndb.BooleanProperty(required=True)
  Tuesday = ndb.BooleanProperty(required=True)
  Wednesday = ndb.BooleanProperty(required=True)
  Thursday = ndb.BooleanProperty(required=True)
  Friday = ndb.BooleanProperty(required=True)
  Saturday = ndb.BooleanProperty(required=True)
  Sunday = ndb.BooleanProperty(required=True)

所以我要做的就是这个。

if newroute.regular:
                RegularDays(route=newroute,
                    Monday=route_json['Days']['Monday'],
                    Tuesday=route_json['Days']['Tuesday'],
                    Wednesday=['Days']['Wednesday'],
                    Thursday=route_json['Days']['Thursday'],
                    Friday=route_json['Days']['Friday'],
                    Saturday=route_json['Days']['Saturday'],
                    Sunday=route_json['Days']['Sunday']).put()

但是现在我对于如何更改此代码非常困惑,因此它可以与NDB一起使用。

谢谢你的帮忙

哈努曼

等值ndbKeyProperty

资料储存库金钥

可选关键字参数:kind = kind,要求分配给此属性的键始终具有指示的种类。可以是字符串或Model子类。

您会发现,只需替换属性名称即可在您的示例中就足够了,除了关键字collection_name不再起作用:这是因为旧版本ReferenceProperty在幕后为您做了一些工作,在引用的类中创建了一个查询属性来尝试使您的生活更轻松,但是它被引入了ndb,选择了一种更明确的方法,即只存储密钥并让您担心实现细节。

如果您想知道,此更改的原因是自动属性是如此易于使用,以至于通常会忽略实际发生的事情(看不见,无所顾忌),并最终导致很多优化这些额外的工作和查询非常耗时。

这对您意味着什么,而不是编写以下行:

for phone in scott.phone_numbers:

您必须phone_numbers自己编写查询代码:)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在Google App Engine上继承db.ReferenceProperty

来自分类Dev

使用MVC5 App将数据库从本地SQL切换到AZURE DB

来自分类Dev

在Google App Engine中切换到自定义域是否会导致AppSpot域停止运行?

来自分类Dev

在Google App Engine中切换到自定义域是否会导致AppSpot域停止运行?

来自分类Dev

Google App Engine NDB查询

来自分类Dev

使用Google App Engine NDB的Odoo | 从Postgresql移植到NDB

来自分类Dev

使用Google App Engine的NDB作为消息队列?

来自分类Dev

从Google App Engine NDB获取图像

来自分类Dev

Google App Engine NDB中的层次结构

来自分类Dev

Google App Engine-NDB-索引问题

来自分类Dev

Google App Engine NDB检索浮动属性

来自分类Dev

如何从Google App Engine NDB更新查询?

来自分类Dev

Google App Engine关于NDB中的密钥

来自分类Dev

Google App Engine-使用key_name或ReferenceProperty进行快速查找

来自分类Dev

使用Google App Engine和ndb时是否看到重复的ID?

来自分类Dev

如何使用Google App Engine NDB得出字段的最大值

来自分类Dev

Google App Engine NDB数据存储区的简单说明

来自分类Dev

google app engine - ndb query to only get a few columns in python

来自分类Dev

Google App Engine ndb.delete_multi()的效率如何?

来自分类Dev

Google App Engine NDB查询和超出的内存

来自分类Dev

在许多位置的Google App Engine NDB查询

来自分类Dev

Google App Engine-NDB-反转IN(列表)的最佳方法

来自分类Dev

使用Maven的Google App Engine端点

来自分类Dev

在Google App Engine上使用php提取

来自分类Dev

使用Google App Engine上传图片失败

来自分类Dev

使用WTForms中的数据填充App Engine NDB实体

来自分类Dev

使用App Engine云端点访问ndb数据存储

来自分类Dev

IPSEC Google App Engine

来自分类Dev

Google App Engine帐单

Related 相关文章

  1. 1

    如何在Google App Engine上继承db.ReferenceProperty

  2. 2

    使用MVC5 App将数据库从本地SQL切换到AZURE DB

  3. 3

    在Google App Engine中切换到自定义域是否会导致AppSpot域停止运行?

  4. 4

    在Google App Engine中切换到自定义域是否会导致AppSpot域停止运行?

  5. 5

    Google App Engine NDB查询

  6. 6

    使用Google App Engine NDB的Odoo | 从Postgresql移植到NDB

  7. 7

    使用Google App Engine的NDB作为消息队列?

  8. 8

    从Google App Engine NDB获取图像

  9. 9

    Google App Engine NDB中的层次结构

  10. 10

    Google App Engine-NDB-索引问题

  11. 11

    Google App Engine NDB检索浮动属性

  12. 12

    如何从Google App Engine NDB更新查询?

  13. 13

    Google App Engine关于NDB中的密钥

  14. 14

    Google App Engine-使用key_name或ReferenceProperty进行快速查找

  15. 15

    使用Google App Engine和ndb时是否看到重复的ID?

  16. 16

    如何使用Google App Engine NDB得出字段的最大值

  17. 17

    Google App Engine NDB数据存储区的简单说明

  18. 18

    google app engine - ndb query to only get a few columns in python

  19. 19

    Google App Engine ndb.delete_multi()的效率如何?

  20. 20

    Google App Engine NDB查询和超出的内存

  21. 21

    在许多位置的Google App Engine NDB查询

  22. 22

    Google App Engine-NDB-反转IN(列表)的最佳方法

  23. 23

    使用Maven的Google App Engine端点

  24. 24

    在Google App Engine上使用php提取

  25. 25

    使用Google App Engine上传图片失败

  26. 26

    使用WTForms中的数据填充App Engine NDB实体

  27. 27

    使用App Engine云端点访问ndb数据存储

  28. 28

    IPSEC Google App Engine

  29. 29

    Google App Engine帐单

热门标签

归档