简体   繁体   English

在新的 Cloud Datastore 模式下实现 ndb.StructuredProperty 的等效性?

[英]Achieving the equivalence of ndb.StructuredProperty in new Cloud Datastore Mode?

In the old ndb Datastore, it is possible to create a StructuredProperty .在旧的ndb Datastore 中,可以创建StructuredProperty This was thanks to the ndb.Model provided.这要归功于提供的ndb.Model

However, in the new Cloud Datastore, the ndb isn't available.但是,在新的 Cloud Datastore 中,ndb 不可用。 So how could we implement a similar functionality as the ndb.StructuredProperty in the new Cloud Datastore?那么我们如何在新的 Cloud Datastore 中实现与 ndb.StructuredProperty 类似的功能呢?

I have a JobApplication model that has one or many Education and Experience objects:我有一个 JobApplication 模型,其中包含一个或多个 Education 和 Experience 对象:

class Education:
    def __init__(self, institution):
        institution = self.institution

class Experience:
    def __init__(self, workplace):
        workplace = self.workplace

class JobApplication:
    def __init__(self):
        self.educations = None
        self.experiences = None

    def add_education(self, education):
        self.educations.append(education)

    def add_experience(self, experience):
        self.experiences.append(experience)        


edu_1 = Education('aol')
edu_2 = Education('myspace')

jobapp = JobApplication()
jobapp.add_education(edu_1)
jobapp.add_education(edu_2)

While this kinda works for reading the entity, it becomes complex while trying to update the entity.虽然这有点适用于reading实体,但在尝试update实体时变得复杂。 For example, how would I be able to update edu_2 within the JobApplication object?例如,我如何能够在JobApplication对象中更新edu_2

Using the old ndb approach is very simple:使用旧的 ndb 方法非常简单:

class Education(ndb.Model):
    institution = ndb.StringProperty()

class JobApplication(ndb.Model)
    education = ndb.StructuredProperty(Education, repeated=True)

How can I achieve this result using the new Cloud Datastore?如何使用新的 Cloud Datastore 实现这一结果?

As noted in the comments, you can use google-cloud-ndb in environments other than first generation App Engine standard runtimes.如评论中所述,您可以在第一代 App Engine 标准运行时以外的环境中使用google-cloud-ndb Thus you can continue to use StructuredProperty.因此您可以继续使用 StructuredProperty。

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

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