【问题标题】:Changing properties of an entity in Google App Engine在 Google App Engine 中更改实体的属性
【发布时间】:2013-12-04 01:07:36
【问题描述】:

我们的设施票请求系统已经上线,但现在我们想编辑与请求实体类型 (requesttype) 关联的属性。当我换行时:

requesttype = db.StringProperty(verbose_name="Request Type*",choices=(["Bulky Item Disposal","Carpentry","Ceiling,Doors","Electrical","Elevator","Fire Equipment","Groundskeeping","Hazardous Waste","HVAC","Kitchen Equipment","Lighting,Painting","Plumbing","Recycling","Refrigeration","Too Cold","Too Hot","Trash","Vehicle Maintenance","Water Leak","Windows","Other"]))

requesttype = db.StringProperty(verbose_name="Request Type*",choices=(["Bulky Item Disposal","Carpentry","Ceiling","Doors","Electrical","Elevator","Fire Equipment","Groundskeeping","Hazardous Waste","HVAC","Kitchen Equipment","Lighting","Locksmith","Painting","Plumbing","Recycling","Refrigeration","Too Cold","Too Hot","Trash","Vehicle Maintenance","Water Leak","Windows","Other"]))

我在日志中收到以下错误;

Property requesttype is u'Ceiling,Doors'; must be one of ['Bulky Item Disposal', 'Carpentry', 'Ceiling', 'Doors', 'Electrical', 'Elevator', 'Fire Equipment', 'Groundskeeping', 'Hazardous Waste', 'HVAC', 'Kitchen Equipment', 'Lighting', 'Locksmith', 'Painting', 'Plumbing', 'Recycling', 'Refrigeration', 'Too Cold', 'Too Hot', 'Trash', 'Vehicle Maintenance', 'Water Leak', 'Windows', 'Other']
    Traceback (most recent call last):
      File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1511, in __call__
        rv = self.handle_exception(request, response, e)
      File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1505, in __call__
        rv = self.router.dispatch(request, response)
      File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1253, in default_dispatcher
        return route.handler_adapter(request, response)
      File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1077, in __call__
        return handler.dispatch()
      File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 547, in dispatch
        return self.handle_exception(e, self.app.debug)
      File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 545, in dispatch
        return method(*args, **kwargs)
      File "/base/data/home/apps/s~mma-facreq/6.372062129927600214/main.py", line 158, in get
        self.response.out.write(template.render(template_values))
      File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/jinja2-2.6/jinja2/environment.py", line 894, in render
        return self.environment.handle_exception(exc_info, True)
      File "/base/data/home/apps/s~mma-facreq/6.372062129927600214/html/manage.html", line 1, in top-level template code
        {% extends "html/base.html" %}
      File "/base/data/home/apps/s~mma-facreq/6.372062129927600214/html/base.html", line 41, in top-level template code
        {% block content %}{% endblock %}
      File "/base/data/home/apps/s~mma-facreq/6.372062129927600214/html/manage.html", line 14, in block "content"
        {% for x in reqs %}
      File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/db/__init__.py", line 2326, in next
        return self.__model_class.from_entity(self.__iterator.next())
      File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/db/__init__.py", line 1438, in from_entity
        return cls(None, _from_entity=entity, **entity_values)
      File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/db/__init__.py", line 970, in __init__
        prop.__set__(self, value)
      File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/db/__init__.py", line 614, in __set__
        value = self.validate(value)
      File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/db/__init__.py", line 2841, in validate
        value = super(StringProperty, self).validate(value)
      File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/db/__init__.py", line 646, in validate
        (self.name, value, self.choices))
    BadValueError: Property requesttype is u'Ceiling,Doors'; must be one of ['Bulky Item Disposal', 'Carpentry', 'Ceiling', 'Doors', 'Electrical', 'Elevator', 'Fire Equipment', 'Groundskeeping', 'Hazardous Waste', 'HVAC', 'Kitchen Equipment', 'Lighting', 'Locksmith', 'Painting', 'Plumbing', 'Recycling', 'Refrigeration', 'Too Cold', 'Too Hot', 'Trash', 'Vehicle Maintenance', 'Water Leak', 'Windows', 'Other']

【问题讨论】:

  • 能否提供用于更改与请求实体关联的属性的代码

标签: python google-app-engine


【解决方案1】:

您的原始代码中似乎有错字,用它来保存一些数据存储条目,然后更新您的代码以修复它。

您现在必须在数据存储中有一些条目包含“Ceiling,Doors”作为请求类型。当它们被获取时,它们会导致错误,因为这不再是合法的请求类型。

要解决此问题,您基本上需要将“Ceiling,Doors”、“Ceiling”、“Doors”添加到选项中,然后将数据存储中具有“Ceiling,Doors”的所有实体更新为适当的,价值。完成此操作后,您可以选择删除“天花板、门”。

【讨论】:

  • 或者使用远程 api 并修改原始值以反映选择。这可以在不部署新代码的情况下完成,但是看到您遇到错误,最好按照@dragonx 执行,使用额外的验证值部署代码。就我个人而言,我不认为对 StringProperty 使用 Choices 是一个好主意,除非您非常确定您永远不会更改选择(尤其是更改/删除现有值)。
  • ^ 那应该是一个and。是的,如果您没有大量实体,远程 API 可能是一种更简单的方法。您仍然需要更新您的模型,否则远程 API 仍将无法处理您的实体,除非您使用低级 API 来更新实体。
  • ;-) 我发现低级 api,通常是更改底层类型最方便的方法,特别是如果您有成千上万的实体。
  • GAE 文档网站上没有记录低级别,您必须在 google/appengine/api/datastore.py btw @TimHoffman 中查看 SDK,您是否实际使用过10'x 数千个实体的远程 API?看起来它会非常缓慢
  • 是的,我知道它没有记录。请参阅这个问题 - stackoverflow.com/questions/19842671/…,我在其中提供了使用低级 api 的示例。是的,我已经通过 remote_api 处理了数万条记录。它很慢,但它确实意味着您可以在几天内逐步处理内容,而不会超出处理预算 - 如果您无法访问计费 C/C 来增加预算,这很有用。实际上 10000 个实体并不慢,通常只有几分钟。尤其是使用批量获取/放置。
猜你喜欢
  • 2015-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-21
  • 1970-01-01
  • 2018-12-31
  • 1970-01-01
  • 2011-04-20
相关资源
最近更新 更多