Reputation: 823
Today I read this article here on how to make One To Many relationship that I use in my GAE REST API.
https://cloud.google.com/appengine/articles/modeling
I thought this was pretty straightforward, but then I realized that I am using ndb and not db, so I cannot use the db.ReferenceProperty.
Here I have a 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 I have my model RegularDays which would be used if Regular in the JSON received is 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 what I would do is simply 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 now I am very confused on how to change this code so it works with NDB.
Thank you for helping
Upvotes: 3
Views: 91
Reputation: 7067
The equivalent in ndb
is the KeyProperty
:
Datastore key
Optional keyword argument: kind=kind, to require that keys assigned to this property always have the indicated kind. May be a string or a Model subclass.
You'll find that simply replacing the property name will be sufficient in your examples, except the keyword collection_name
will not work anymore: that's because the old ReferenceProperty
did some work for you behind-the-scenes, creating a query property in the referenced class to try make your life easier, but it was dropped in ndb
, opting for a more explicit approach of just storing keys and letting you worry about implementation details.
In case you're wondering, the reason for this change is that the automatic properties were so easy to use that it was common to ignore what was really going on (out of sight, out of mind), and ended up with a lot of extra work and queries that were time-consuming to optimize.
What this means for you is that instead of writing this line:
for phone in scott.phone_numbers:
You'll have to code the phone_numbers
query yourself :)
Upvotes: 6