【问题标题】:How to query for a Computed Property inside a Structured Property in GAE Python如何在 GAE Python 中查询结构化属性中的计算属性
【发布时间】:2013-12-27 17:09:23
【问题描述】:
class Name_score(ndb.Model): 
    def get_value_of_name(self, name, date): 
        # concatenate "X" and date before returning 
        return_text = "X"+name+str(date) 
        return return_text 

    date = ndb.DateTimeProperty() 
    name = ndb.StringProperty() 
    # Computed values 
    name_value_computed = ndb.ComputedProperty(lambda e: e.get_value_of_name(e.name, e.date)) 

class Activity_db(ndb.Model): 
    # contains many properties 
    # removed the not relevant ones here 
    name_set = ndb.StructuredProperty(Name_score, repeated=True) 
    hobby = ndb.StringProperty() 

NDB 中有很多条目。我想获取特定日期的记录,“name_value_computed”与查询中提供的数据匹配。
要查询具有特定“name_value_computed”和“date”值(如下示例)的所有此类条目,查询将是什么。

示例(条件算法):
(“爱好”是“网球”)AND((如果在“日期”“18/01/1900”,“name_value_computed”是“XJohn18/01/1900”)或(如果在“日期”“22/04/1910” ", "name_value_computed" 是 "XBran22/04/1910"))
以下是否正确:

date_1 = datetime.strptime("18/01/1900", '%d/%m/%Y') 
name_computed_value_1 = "XJohn18/01/1900" 
date_2 = datetime.strptime("22/04/1910", '%d/%m/%Y') 
name_computed_value_2 = "XBran22/04/1910" 
qry_1 = Activity_db.query(ndb.OR(Activity_db.name_set==Name_score(date=date_1, name_value_computed=name_computed_value_1), Activity_db.name_set==Name_score(date=date_2, name_value_computed=name_computed_value_2)), Activity_db.hobby=="tennis") 
record_list = qry_1.fetch() 

我收到以下错误:

File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\model.py", line 2745, in __init__

    self._set_attributes(kwds)

File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\model.py", line 2791, in _set_attributes

    prop._set_value(self, value)

File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\model.py", line 2612, in _set_value

    raise ComputedPropertyError("Cannot assign to a ComputedProperty")

ComputedPropertyError: Cannot assign to a ComputedProperty

我参考了 Google Python GAE 页面的this section

【问题讨论】:

  • IIRC,它应该类似于qry_1 = Activity_db.query(ndb.AND(Activity_db.name_set.date == date_1, Activity_db.name_set.name_value_computed == "nhoJ1"), Activity_db.hobby = "tennis")。实际上,在这里调用ndb.AND 是多余的,因为您的所有条件都是“和”在一起的......
  • 感谢 mgilson 的回复。您在这里正确指出了不必要的“ndb.AND”(现在已删除)。我的实际查询包含更多条件。但是,我试图在这里提供一个较小的版本(类和查询),只关注它的有问题的部分。我现在试图让我的问题更清楚
  • @mgilson 小心使用 AND。从文档中可以看出,重复字段的两个子属性之间的 AND 在此处的行为不符合预期,因为它可以匹配两个单独的属性。

标签: python google-app-engine google-cloud-datastore app-engine-ndb


【解决方案1】:

似乎没有任何方法可以直接执行此操作,因为您无法明确指定计算属性的值。

我认为您最好的选择是将您的计算值存储为StringProperty,以便此查询可以工作。您可以使用pre-put hook 复制计算属性的一些功能。当您将实体放入数据存储区时,您的挂钩可以填充 name_value_computed 字段。

您的模型将如下所示:

class Name_score(ndb.Model): 
  def get_value_of_name(self, name, date): 
    # concatenate "X" and date before returning 
    return_text = "X"+name+str(date) 
    return return_text 

  def _pre_put_hook(self):
    self.name_value_computed = self.get_value_of_name(self.name, self.date)

  date = ndb.DateTimeProperty() 
  name = ndb.StringProperty() 
  # Computed values 
  name_value_computed = ndb.StringProperty()

您必须小心,如果您使用此方法,您需要在访问name_value_computed 字段之前put() 您的模型,否则它不会被设置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-07
    • 1970-01-01
    相关资源
    最近更新 更多