App Engine,两个实体之间的交叉引用

发布于 2024-10-05 18:50:43 字数 398 浏览 0 评论 0原文

我希望有两种类型的实体相互引用。 但Python还不知道第一个实体类中第二个实体类的名称。 那么我该如何编码。

class Business(db.Model):
  bus_contact_info_ = db.ReferenceProperty(reference_class=Business_Info)

class Business_Info (db.Model):
  my_business_ =  db.ReferenceProperty(reference_class=Business)

如果您建议仅在一个中使用引用并使用隐式创建的属性 (这是一个查询对象)在其他中。 然后我质疑使用查询与直接使用 get() 键的 CPU 配额惩罚

请建议如何在 python 中编写此代码

i will like to have two types of entities referring to each other.
but python dont know about name of second entity class in the body of first yet.
so how shall i code.

class Business(db.Model):
  bus_contact_info_ = db.ReferenceProperty(reference_class=Business_Info)

class Business_Info (db.Model):
  my_business_ =  db.ReferenceProperty(reference_class=Business)

if you advice to use reference in only one and use the implicitly created property
(which is a query object) in other.
then i question the CPU quota penalty of using query vs directly using get() on key

Pleas advise how to write this code in python

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你好,陌生人 2024-10-12 18:50:43

查询速度稍慢,因此确实使用了更多资源。 ReferenceProperty 不需要参考类。因此,您始终可以像这样定义业务:

class Business(db.Model):
  bus_contact_info_ = db.ReferenceProperty()

您的数据结构也可能有更好的选择。请查看建模关系一文,了解一些想法。

这是一对一的映射吗?如果这是一对一的映射,您最好对数据进行非规范化。

它会改变吗?如果不是(并且是一对一的),也许您可​​以使用实体组并构建数据,以便您可以直接使用键/键名称。您可以通过将 BusinessInfo 设为 Business 的子项,然后始终使用“i”作为 key_name 来实现此目的。例如:

business = Business().put()
business_info = BusinessInfo(key_name='i', parent=business).put()

# Get business_info from business:
business_info = db.get(db.Key.from_path('BusinessInfo', 'i', parent=business))

# Get business from business_info:
business = db.get(business_info.parent())

Queries are a little slower, and so they do use a bit more resources. ReferenceProperty does not require reference_class. So you could always define Business like:

class Business(db.Model):
  bus_contact_info_ = db.ReferenceProperty()

There may also be better options for your datastructure too. Check out the modelling relationships article for some ideas.

Is this a one-to-one mapping? If this is a one-to-one mapping, you may be better off denormalizing your data.

Does it ever change? If not (and it is one-to-one), perhaps you could use entity groups and structure your data so that you could just directly use the keys / key names. You might be able to do this by making BusinessInfo a child of Business, then always use 'i' as the key_name. For example:

business = Business().put()
business_info = BusinessInfo(key_name='i', parent=business).put()

# Get business_info from business:
business_info = db.get(db.Key.from_path('BusinessInfo', 'i', parent=business))

# Get business from business_info:
business = db.get(business_info.parent())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文