简体   繁体   English

Google App Engine数据存储区建模问题

[英]Google App Engine Datastore modeling issue

There has been tons of questions on how to properly do data modeling when moving from traditional relational database to non-relational database such as the App Engine Datastore. 从传统的关系型数据库迁移到非关系型数据库(例如App Engine数据存储区)时,如何正确进行数据建模一直存在大量问题。

I have read the NDB official documentation many times and tried a couple of tutorials on the internet, however, I am still stucked. 我已经阅读了NDB的官方文档很多次,并尝试了Internet上的一些教程,但是,我仍然很困惑。

I am developing a financial application that captures all my personal expenses. 我正在开发一种财务应用程序,该程序可以捕获我的所有个人支出。 This means I have designed an entity called Expense with a few properties on it. 这意味着我设计了一个名为Expense的实体,上面带有一些属性。 As part of an Expense, I also designed a Vendor entity to store information related to merchant. 作为费用的一部分,我还设计了一个Vendor实体来存储与商人有关的信息。

Here is the implementation: 这是实现:

class Expense(ndb.Model):
    amount = ndb.FloatProperty(required=False)
    description = ndb.StringProperty()
    vendor = ndb.KeyProperty(kind=Vendor)

class Vendor(ndb.Model):
    companyName = ndb.StringProperty(required=False)
    friendlyName = ndb.StringProperty(required=False, default="")

This is the code to retrieve the Expense information: 这是用于检索费用信息的代码:

def get(self):
    template_page = "expense.html"
    template_values = {'expense':Expense.query()}

    self.renderTemplatePage(template_page, template_values)

The problem I am facing with the design above is how to retreive Expense and Vendor information and pass it to JINJA to render it. 我上面的设计面临的问题是如何检索费用和供应商信息并将其传递给JINJA进行渲染。 As I can expect, the Vendor property in the Expense class is just a Key and not the object itself. 如我所料,Expense类中的Vendor属性只是一个Key,而不是对象本身。 So JINJA cannot understand the expression {{expense.vendor.friendlyName}} 因此,JINJA无法理解表达式{{expense.vendor.friendlyName}}

Although the problem seems to be technical, I believe my dificulty is related to the concept of non-relational database and the traditional-like design I applied to the solution. 尽管问题似乎是技术性的,但我相信我的困难与非关系数据库的概念以及应用于该解决方案的类似传统的设计有关。

An alternative is to use StructuredProperty instead of KeyPrperty. 一种替代方法是使用StructuredProperty代替KeyPrperty。 However, one solution requirement is to be able to rename a vendor friendly name and that should affect all exiting expenses. 但是,一个解决方案要求是能够重命名供应商友好名称,这将影响所有现有费用。 It is insane to go after each stuctured property and find the changes. 追逐每个结构化属性并找到更改是疯狂的。

Before giving up on App Engine and moving to Google Cloud SQL, I would like to hear some advice on how to solve this data modeling scenario. 在放弃App Engine并转而使用Google Cloud SQL之前,我想听听有关如何解决此数据建模方案的一些建议。 Maybe the solution I need to build is not recommended for non-relational database. 对于非关系数据库,也许不建议我需要构建的解决方案。

Thansk for any support on that. 坦斯克对此表示支持。

In your template you could do: 在您的模板中,您可以执行以下操作:

{{ expense.vendor.get().friendlyName }}

Or you could add a property to your model: 或者,您可以向模型添加属性:

class Expense(ndb.Model):
    amount = ndb.FloatProperty(required=False)
    description = ndb.StringProperty()
    vendor = ndb.KeyProperty(kind=Vendor)

    @property
    def vendor_name(self):
      return self.vendor.get().friendlyName

That will allow in your template: 这将允许您在模板中:

{{ expense.vendor_name }}

To stop this being so inefficient, you can fetch the vendors before you display your expense-list, making the calls to get inside the loop just local-memory lookups: 为了制止这种如此低效的,您可以在显示您的费用列表之前获取的供应商,使得调用get循环只是本地内存中查找里面:

def get(self):
    template_page = "expense.html"
    expenses = Expense.query().fetch()
    ndb.get_multi([e.vendor for e in expenses])
    template_values = {'expense':expenses}

    self.renderTemplatePage(template_page, template_values)

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

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