python - 获取行上的所有字段值

标签 python google-app-engine

我使用下面的代码来获取一行中的所有值。

from google.appengine.ext import db
from python_lib.models import Field

field = Field.all()[0]
names = Field.properties()

for key in names:
  print field.get(key)

但它给出了以下错误,

BadKeyError: Invalid string key name.

最佳答案

您在这段代码中混淆了太多不同的 api。

for key in names:
   print field.get(key)

您的 get() 调用无效,因为您实际上正在尝试调用类方法来从数据存储区获取实体 - 请参阅此方法的文档 https://developers.google.com/appengine/docs/python/datastore/modelclass#Model_get

要从字段实例(您的对象)中按名称获取属性,您应该使用 getattr

for key in names:
    print getattr(field,key)

或者全部交替使用属性对象的get_value_for_datastore(model_instance),该属性对象是从properties()调用中返回的以及属性的名称。

关于python - 获取行上的所有字段值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15053792/

相关文章:

类和模块方法的 Pythonic 使用

java - 将 jOOQ 与 Google Cloud SQL 结合使用

python - 如何使用bulkloader上传日期?

python - 在 Windows 上用 Python 进行文学编程的最佳方法是什么?

python - `pip search` 如何对结果进行排序?

ruby-on-rails - 没有经验的 Rails 用户 : Wanting to Connect PostgreSQL to Already Existing App: Server Will not Connect

google-app-engine - 如何找到两年前部署的谷歌应用引擎源代码

安卓 : GCM application Server maintaining registration ids

python - 应用引擎 'explicitly cancelled' 错误

python - 使用 Anaconda 创建 python 环境的最佳实践工作流程是什么?