未正确计算计算属性的NDB查询

2024-04-25 02:09:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我遇到了一个奇怪的问题。我想我不是在replication issue的情况下,而是类似的情况。在

我有一个计算属性的模型

   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"

现在,我查询所有的模型,我做这两个打印

^{pr2}$

结果显示不同的值:

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

在第一次打印中status=ONGOING,而在第二次打印中是{},因为{}是在今天之后。在

这对查询有影响,因为如果我查询status=='FINISHED',它不会返回正确的结果集。在

有什么想法吗?在

编辑:

实际上医生说:

Caution: ComputedProperties are not calculated on query, but rather on put(). If you update a model's schema to include a ComputedProperty, you should remember to update existing entities by loading and writing them to the Datastore. See Updating Your Model's Schema for more information.

但是,如何执行查询呢?我要用“手”过滤吗?在


Tags: to模型selfnonedatetimedatereturnif
1条回答
网友
1楼 · 发布于 2024-04-25 02:09:23

您必须按datetime属性进行查询,因为当您将此实体放入数据存储时,该值是根据当前时间计算的,而当您查询它时,旧值仍在使用,但当您触摸此属性时,值将重新计算。在

相关问题 更多 >