python - Switching from db.ReferenceProperty to ndb with Google App Engine? -
today read article here on how make 1 many relationship use in gae rest api.
https://cloud.google.com/appengine/articles/modeling
i thought pretty straightforward, realized using ndb , not db, cannot use db.referenceproperty.
here have model route
class route(ndb.model): driver_id = ndb.integerproperty() requester_id = ndb.integerproperty() startpoint = ndb.geoptproperty(required=true) endpoint = ndb.geoptproperty(required=true) regular = ndb.booleanproperty(required=true) date_and_time = ndb.datetimeproperty(required=true) places_available = ndb.integerproperty() val_lift = ndb.integerproperty()
and here have model regulardays used if regular in json received true
class regulardays(ndb.model): route = db.referenceproperty(route, collection_name='regular_days') monday = ndb.booleanproperty(required=true) tuesday = ndb.booleanproperty(required=true) wednesday = ndb.booleanproperty(required=true) thursday = ndb.booleanproperty(required=true) friday = ndb.booleanproperty(required=true) saturday = ndb.booleanproperty(required=true) sunday = ndb.booleanproperty(required=true)
so this.
if newroute.regular: regulardays(route=newroute, monday=route_json['days']['monday'], tuesday=route_json['days']['tuesday'], wednesday=['days']['wednesday'], thursday=route_json['days']['thursday'], friday=route_json['days']['friday'], saturday=route_json['days']['saturday'], sunday=route_json['days']['sunday']).put()
but confused on how change code works ndb.
thank helping
the equivalent in ndb
keyproperty
:
datastore key
optional keyword argument: kind=kind, require keys assigned property have indicated kind. may string or model subclass.
you'll find replacing property name sufficient in examples, except keyword collection_name
not work anymore: that's because old referenceproperty
did work behind-the-scenes, creating query property in referenced class try make life easier, dropped in ndb
, opting more explicit approach of storing keys , letting worry implementation details.
in case you're wondering, reason change automatic properties easy use common ignore going on (out of sight, out of mind), , ended lot of work , queries time-consuming optimize.
what means instead of writing line:
for phone in scott.phone_numbers:
you'll have code phone_numbers
query :)
Comments
Post a Comment