【发布时间】:2016-12-24 21:01:47
【问题描述】:
我正在生成一个DocType 类,用于基于我的 ORM 构建映射和保存文档。
def get_doc_type(self):
attributes = {}
...
# Build attributes dictionary here
DT = type('DocType', (DocType,), attributes)
return DT
这似乎工作正常,我对映射没有任何问题。我的问题是当我尝试保存文档时。
这不起作用
Doc = get_doc_type()
for instance in queryset:
doc = Doc()
for field_name in fields:
attribute = getattr(instance, field_name, None)
setattr(doc, field_name, attribute)
doc.save(index)
发生这种情况时,文档确实会被保存,但是我的任何属性都没有设置。它只是一个空文档。
我已调试代码以确认field_name 和attribute 包含我期望的值。
这行得通
Doc = self.get_doc_type()
for instance in queryset:
kwargs = {}
for field_name in fields:
attribute = getattr(instance, field_name, None)
kwargs.update({field_name: attribute})
doc = Doc(**kwargs)
doc.save(index=index)
当我使用此策略时,文档按预期保存,所有信息和attributes都已从我的instance传递到doc。
问题
这可能是什么原因造成的?我不明白为什么这两种策略都无效。
【问题讨论】:
标签: python elasticsearch keyword-argument setattr elasticsearch-dsl