Invalid NDB request for a computed property

I am having a weird problem. I think I am not in the case of a replication problem , but something similar.

I have this model which has a computed property

   status = ndb.ComputedProperty(lambda self: self._compute_status())
...
    def _compute_status(self):
        if self.canceled:
            return "CANCELED"
        course = self.course.get().course_type
        if course == "SCHEDULED":
            now = datetime.now()
            if now < self.start_date:
                return "UPCOMING"
            elif now > self.end_date:
                return "FINISHED"
        return "ONGOING"

      

Now I query all models and I make these two prints

print sessions[0]
print sessions[0].status

      

the results show different meanings:

Session(key=Key('Session', 5302669702856704), canceled=False, course=Key('Course', 6605041225957376), created=datetime.datetime(2015, 5, 5, 13, 39, 56, 86329), day_no=None, end_date=datetime.datetime(2015, 5, 8, 9, 35), meta_data=None, name=u'asd', profile=None, session_type=u'JOINT', start_date=datetime.datetime(2015, 5, 7, 8, 50), status='UPCOMING', url=u'', week_no=None)
FINISHED

      

On the first line, type status=ONGOING

, and on the second FINISHED

, as it should be, since it end_date

is after today.

This matters to the query, because if I query status=='FINISHED'

it does not return the correct result set.

Any idea?

EDIT:

In fact, the docs say:

Note: ComputedProperties is not computed on demand, but rather put (). If you update your model schema to include ComputedProperty, you must remember to update existing objects by loading and writing them to the data store. For details, see Updating the schema model information.

but then how can i fulfill the request? should I filter them by hand?

+3


source to share


1 answer


You have to query the datetime property because when you put this object into the data store, the value is calculated based on the current time, and when you query it, the old value is still in use, but the value gets recalculated when this property is touched.



+3


source







All Articles