简体   繁体   中英

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

In the old ndb Datastore, it is possible to create a StructuredProperty . This was thanks to the ndb.Model provided.

However, in the new Cloud Datastore, the ndb isn't available. So how could we implement a similar functionality as the ndb.StructuredProperty in the new Cloud Datastore?

I have a JobApplication model that has one or many Education and Experience objects:

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. For example, how would I be able to update edu_2 within the JobApplication object?

Using the old ndb approach is very simple:

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?

As noted in the comments, you can use google-cloud-ndb in environments other than first generation App Engine standard runtimes. Thus you can continue to use StructuredProperty.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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