简体   繁体   中英

In App Engine for Python, is it possible to persist a class with another object nested inside it?

In App Engine for Python, is there anything like Objectify (Java Library) where I can easily embed a class within another and save it to the datastore?

This class would be modeled like the following example where a Venue contain a Location object. I want to persist this as one nested object, as well as be able to query by fields in the embedded object.

class Location():
city = db.StringProperty()
state = db.StringProperty()

class Venue(db.Model):
name = db.StringProperty()
location = Location()

Here is information on how it works in Objectify in App Engine for Java.
http://code.google.com/p/objectify-appengine/wiki/IntroductionToObjectify#@Embedded

Is this possible using Python?

Consider using Reference properties. Ie store a Location object as its own entity and incorporate that location into the Venue object by reference.

class Location():
  city = db.StringProperty()
  state = db.StringProperty()

class Venue(db.Model):
  name = db.StringProperty()
  location = db.ReferenceProperty(Location)

Then, if you want to transact on a Location and Venue at the same time, use datastore transactions.

EDIT: To query fields in the 'contained' object, use datastore "back references". Ie the fact that Venue contains a reference to Location means Location also contains references to Venues. See: http://code.google.com/appengine/docs/python/datastore/datamodeling.html#References

Not currently, but the NDB library supports embedding models within one another either by serializing them as Protocol Buffers, or by nesting their properties (Objectify fashion).

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