gql specific aggregate function
assume the following:
class events(db.model):
eventDate = db.DateProperty()
eventItem = db.ReferenceProperty(items)
class items(db.model):
itemCode = db.IntegerProperty()
itemTitle = db.StringProperty()
now if i have multiple events in the future for a specific item, how can i show a list of all items with only the next occurring event shown for each item?
i have tried an aggregate field in the item class as:
itemNext = db.DateProperty()
which gets updated when an event is set but the problem with this 开发者_如何转开发is what happens when an event is changed or deleted lots more calls have to be made to empty it or change it (unless i am missing something)
Is it the case that the next event is the event who's date is in the future and of those dates in the future is closest to right now? If so, I would do something like this:
def get_next(for_item):
next_event = events.all().filter('eventItem = ', for_item.key()).filter('eventDate >', datetime.datetime.now()).order('eventDate').get()
return next_event
Your best option is to keep a reference to the next event, and do a datastore lookup for it. Yes, that will require updating those records when an associated item is modified or deleted.
精彩评论