如何将 ReferenceProperty 视为 AppEngine 数据存储区中的布尔值?

发布于 2024-12-06 11:02:06 字数 856 浏览 1 评论 0原文

我有一个模型,它可能引用另一个模型,并且有一个completed_on 日期。

class Game(db.Model):
    challenge = db.ReferenceProperty(Challenge, required=False)
    completed_on = db.DateTimeProperty(default=None)

我希望能够选择在特定时间段之前完成的所有已完成的挑战游戏。问题是我不能有两个不等式

但是http://code.google.com/appengine/ docs/python/datastore/queries.html 说:

仅允许在一个属性上使用不等式过滤器

这意味着我无法进行挑战 > '' 和 Completed_on < datetime_value

但是我可以做 is_challenge=True 和completed_on datetime_value 假设我向数据库添加了一个名为 is_challenge 的新列。考虑到这一点,是否有办法说服数据存储将挑战视为布尔值?

看来不能以这种方式完成,我希望有其他方法可以完成此任务,而不必向模型添加另一列,并且可能还需要与该列一起添加另一个索引。

我希望游戏列表足够大,以至于不想在 python 中为显示的每个页面进行过滤。

I have a model which might reference another model and which has a completed_on date.

class Game(db.Model):
    challenge = db.ReferenceProperty(Challenge, required=False)
    completed_on = db.DateTimeProperty(default=None)

I'd like to be able to select all of the completed Challenge games that were completed before a certain time period. Problem is I can't have 2 inequalities

But http://code.google.com/appengine/docs/python/datastore/queries.html says:

Inequality Filters Are Allowed on One Property Only:

Which means I can't do challenge > '' and completed_on < datetime_value

However I could do is_challenge=True and completed_on < datetime_value provided I add a new column to the db called is_challenge. With that in mind, is there someway to convince the datastore to treat challenge as a boolean?

It appears it can't be done this way, and I'm hoping there is some other way to accomplish this without having to add yet another column to the model and probably yet another index to go with that column.

I am expecting the list of games to be large enough to not want to filter it in python for every page displayed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愛上了 2024-12-13 11:02:06

正如您所观察到的,做到这一点的唯一方法是添加一个布尔属性来指示是否设置了挑战。您可以使用 计算属性

class Game(db.Model):
    challenge = db.ReferenceProperty(Challenge, required=False)
    completed_on = db.DateTimeProperty(default=None)

    @db.ComputedProperty
    def has_challenge(self):
      return self.challenge is not None

The only way to do this is, as you observe, to add a boolean property that indicates if challenge is set or not. You can do this with ease by using ComputedProperty:

class Game(db.Model):
    challenge = db.ReferenceProperty(Challenge, required=False)
    completed_on = db.DateTimeProperty(default=None)

    @db.ComputedProperty
    def has_challenge(self):
      return self.challenge is not None
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文