小编tij*_*ijs的帖子

在App Engine数据存储上查询等效?

我有一个包含IP地址范围的模型,类似于:

class Country(db.Model):
  begin_ipnum = db.IntegerProperty()
  end_ipnum = db.IntegerProperty()
Run Code Online (Sandbox Code Playgroud)

在SQL数据库上,我将能够找到包含特定范围内的IP的行,如下所示:

SELECT * FROM Country WHERE ipnum BETWEEN begin_ipnum AND end_ipnum
Run Code Online (Sandbox Code Playgroud)

或这个:

SELECT * FROM Country WHERE begin_ipnum < ipnum AND end_ipnum > ipnum
Run Code Online (Sandbox Code Playgroud)

遗憾的是,GQL只允许在一个属性上使用不等式过滤器,并且不支持BETWEEN语法.我如何解决这个问题,并在App Engine上构建一个与这些相当的查询?

此外,ListProperty创建记录时可以"生存"还是必须计算?

首先尝试解决方案更新问题:

所以根据David的答案以及以下文章:

http://appengine-cookbook.appspot.com/recipe/custom-model-properties-are-cute/

我正在尝试将自定义字段添加到我的模型中,如下所示:

class IpRangeProperty(db.Property):
  def __init__(self, begin=None, end=None, **kwargs):
    if not isinstance(begin, db.IntegerProperty) or not isinstance(end, db.IntegerProperty):
        raise TypeError('Begin and End must be Integers.')
    self.begin = begin
    self.end = end
    super(IpRangeProperty, self).__init__(self.begin, self.end, **kwargs)

  def get_value_for_datastore(self, model_instance):
    begin …
Run Code Online (Sandbox Code Playgroud)

google-app-engine gql google-cloud-datastore

5
推荐指数
1
解决办法
1909
查看次数