我有以下结构:
structure = {
'firstname': basestring,
'lastname': basestring,
'genres': [basestring],
'address': [
{'number': basestring, 'street': basestring, 'town': basestring}
],
'phone': [
{'type': basestring, 'number': basestring}
],
}
我有一个小的辅助方法来遍历游标以返回一个 python dict,如下所示:
def to_django_context(cursor):
records = []
for r in cursor:
records.append(r.to_json_type())
return records
这工作正常,直到我想向结构中添加另一个嵌套字段,如下所示:
structure = {
'firstname': basestring,
'lastname': basestring,
'genres': [basestring],
'address': [
{'number': basestring, 'street': basestring, 'town': basestring}
],
'phone': [
{'type': basestring, 'number': basestring}
],
'title': [{'TEST_FIELD': basestring}],
}
此时我的光标迭代器因 KeyError 而失败。如果我删除集合中的所有文档,它会正常工作。那么这是否意味着每次我更改文档结构对象时,我都必须删除集合?
干杯,F