在AppEngine Datastore中,如何将ReferenceProperty像布尔值对待? [英] How do you treat a ReferenceProperty like a boolean in AppEngine Datastore?

查看:101
本文介绍了在AppEngine Datastore中,如何将ReferenceProperty像布尔值对待?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  class Game(db.Model)这个模型可以引用另一个模型,并且有一个completed_on日期。 :
challenge = db.ReferenceProperty(Challenge,required = False)
completed_on = db.DateTimeProperty(default = None)

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


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



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

这意味着我可以'不要做挑战> ''和completed_on< datetime_value



然而,我可以做 is_challenge = True并且completed_on< datetime_value 提供了一个名为is_challenge的db新列。考虑到这一点,有没有办法说服数据存储将挑战视为一种布尔?

看起来不能这样做,我希望还有其他一些方法可以实现这一点,而无需在模型中添加另一列,并且可能还有另一个与该列一起使用的索引。



我期待着游戏足够大,不想在python中为每个显示的页面过滤它。

解决方案

唯一的方法是,如您所见,添加一个布尔属性,指示是否设置了 challenge 。您可以使用 ComputedProperty

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

@ db.ComputedProperty
def has_challenge(self):
返回self.challenge不是


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.

解决方案

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

这篇关于在AppEngine Datastore中,如何将ReferenceProperty像布尔值对待?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆