Reputation: 3123
Below is python code where I am trying to get reservations information from the Reservations Model.
i=0
for c in courts:
court = names[i]
i=i+1
c_key=c.key()
logging.info("c_key: %s " % c_key)
weekday_key= db.Key.from_path('Courts', 'c_key', 'Days', weekday)
logging.info("weekday_key: %s " % weekday_key)
logging.info("weekday: %s " % weekday)
logging.info("court: %s " % court)
reservation = db.Query(Reservations)
nlimit=2*len(times)
reservations = reservation.fetch(limit=nlimit)
logging.info("reservations: %s " % len(reservations))
There are only two court entities in my Courts database, court1 and court2.
There also only 14 weekday entities in my Days database, 7 for court1 and 7 for court2, named Sunday, ... , Saturday. In the current example I am trying to get the key for the 2 Monday Days, one for court1 and one for court2.
I don't understand why according to the log below, I am getting the same weekday_key
for the two different courts which have different keys c_key
themselves.
In the log below, whether I put into the db.Key.from_path(
command 'c_key' or 'court' I get exactly the same result, which shows that the values of the 2 weekday_key
s are identical, not different as I expected.
INFO 2012-09-10 21:25:19,189 views.py:226] c_key: ag1kZXZ-c2NoZWR1bGVycicLEglMb2NhdGlvbnMiBlJvZ2VycwwLEgZDb3VydHMiBmNvdXJ0MQw
INFO 2012-09-10 21:25:19,189 views.py:228] weekday_key: ag1kZXZ-c2NoZWR1bGVyciELEgZDb3VydHMiBWNfa2V5DAsSBERheXMiBk1vbmRheQw
INFO 2012-09-10 21:25:19,189 views.py:229] weekday: Monday
INFO 2012-09-10 21:25:19,189 views.py:230] court: court1
INFO 2012-09-10 21:25:19,192 views.py:235] reservations: 1
INFO 2012-09-10 21:25:19,192 views.py:226] c_key: ag1kZXZ-c2NoZWR1bGVycicLEglMb2NhdGlvbnMiBlJvZ2VycwwLEgZDb3VydHMiBmNvdXJ0Mgw
INFO 2012-09-10 21:25:19,192 views.py:228] weekday_key: ag1kZXZ-c2NoZWR1bGVyciELEgZDb3VydHMiBWNfa2V5DAsSBERheXMiBk1vbmRheQw
INFO 2012-09-10 21:25:19,192 views.py:229] weekday: Monday
INFO 2012-09-10 21:25:19,192 views.py:230] court: court2
INFO 2012-09-10 21:25:19,195 views.py:235] reservations: 1
My Models are as follows.
class Courts(db.Model): #parent is Locations, courtname is key_name
location = db.ReferenceProperty(Locations)
timezone = db.StringProperty()
class Days (db.Model): #parent is Courts, name is key_name, day of week
court = db.ReferenceProperty(Courts)
startTime = db.ListProperty(int)
endTime = db.ListProperty(int)
class Reservations(db.Model): #parent is Days, hour, minute HH:MM is key_name
weekday = db.ReferenceProperty(Days)
day = db.IntegerProperty()
nowweekday = db.IntegerProperty()
name = db.StringProperty()
hour = db.IntegerProperty()
minute = db.IntegerProperty()
Upvotes: 0
Views: 378
Reputation: 9116
How do I construct a query for all Reservations on a specific day in Days at a specific court in Courts on a specific weekday in (week)Days?
You know the values for the keys so you make up a key by hand (so to speak) and then make your query with that key as the ancestor.
So for example:
key = ndb.Key(BlogPost, 12345)
qry = Comment.query(ancestor=key)
but here you'd use something like
key = ndb.key(Locations, "Place1", Courts, "Name_Of_Court")
result = Reservations.query(ancestor=key)
and so on, so you are working your way down the chain and building the key with all the information you have (i.e what court they want to reserve). Then the results of your ancestor query will be those models that have the key you passed as their ancestors.
https://developers.google.com/appengine/docs/python/ndb/queries#ancestor
Upvotes: 1
Reputation: 3123
i=0
for c in courts:
court_id = names[i]
i=i+1
weekday_key = db.Key.from_path('Courts', c.key().name(), 'Days', weekday)
reservation=Reservations.all()
reservation.ancestor(weekday_key)
nlimit=2*len(times)
reservations = reservation.fetch(limit=nlimit)
What I don't like about this answer is that weekday_key
is the same for all c in courts. That does not seem right.
Upvotes: 1
Reputation: 600059
You're calculating the keys using the string 'c_key'
each time, not the value of the variable c_key
.
However even if you fix this it still won't work, since you want the ID of the court, not the full key path.
Upvotes: 1