我使用Flask-Eve为我的数据提供API。我想使用Eve插入我的记录,以便获得_created属性和其他Eve添加的属性。
我的两个字段是dicts,一个是列表。当我试图将它插入Eve时,结构似乎变得平坦,失去了一些信息。试着告诉夏娃关于这个词典和&列表元素在POST上给我一个错误,说这些字段需要是dicts和列表,但它们已经是!有人可以帮助我吗?告诉我我做错了什么?
My Eve conf看起来像这样:
'myendpoint': { 'allow_unknown': True,
'schema': { 'JobTitle': { 'type': 'string',
'required': True,
'empty': False,
'minlength': 3,
'maxlength': 99 },
'JobDescription': { 'type': 'string',
'required': True,
'empty': False,
'minlength': 32,
'maxlength': 102400 },
},
},
但是当我使用请求发布以下结构时:
{
"_id" : ObjectId("56e840686dbf9a5fe069220e"),
"Salary" : {
"OtherPay" : "On Application"
},
"ContactPhone" : "xx",
"JobTypeCodeList" : [
"Public Sector",
"Other"
],
"CompanyName" : "Scc",
"url" : "xx",
"JobTitle" : "xxx",
"WebAdID" : "TA7494725_1_1",
"JobDescription" : "xxxx",
"JobLocation" : {
"DisplayCity" : "BRIDGWATER",
"City" : "BRIDGWATER",
"StateProvince" : "Somerset",
"Country" : "UK",
"PostalCode" : "TA6"
},
"CustomField1" : "Permanent",
"CustomField3" : "FTJOBUKNCSG",
"WebAdManagerEmail" : "xxxx",
"JobType" : "Full",
"ProductID" : "JCPRI0UK"
}
帖子行如下:
resp = requests.post(url, data = job)
它变得扁平化了#39;并丢失来自dicts和list的信息:
{
"_id" : ObjectId("56e83f5a6dbf9a6395ea559d"),
"Salary" : "OtherPay",
"_updated" : ISODate("2016-03-15T16:59:06Z"),
"ContactPhone" : "xx",
"JobTypeCodeList" : "Public Sector",
"CompanyName" : "Scc",
"url" : "xxx",
"JobTitle" : "xx",
"WebAdID" : "TA7494725_1_1",
"JobDescription" : "xxx",
"JobLocation" : "DisplayCity",
"CustomField1" : "Permanent",
"_created" : ISODate("2016-03-15T16:59:06Z"),
"CustomField3" : "FTJOBUKNCSG",
"_etag" : "55d8d394141652f5dc2892a900aa450403a63d10",
"JobType" : "Full",
"ProductID" : "JCPRI0UK"
}
我尝试更新我的架构,说一些是dicts和list:
'JobTypeCodeList': { 'type': 'list'},
'Salary': { 'type': 'dict'},
'JobLocation': { 'type': 'dict'},
但是当我在新记录中发帖时,我得到一个错误说
{u'Salary': u'must be of dict type', u'JobTypeCodeList': u'must be of list type', u'JobLocation': u'must be of dict type'},
我在POST之前验证了type(job.Salary) == dict
等,所以我不确定如何解决这个问题。虽然我可以将记录直接发布到MongoDB中,但是绕过夏娃,如果可能的话,我更愿意使用Eve。
答案 0 :(得分:0)
如果这对其他人有用,我最后通过在Eve中发布一个平面结构来解决这个问题,然后使用on_insert和on_update事件来遍历键并从中构造对象(和列表)。
它有点令人费解,但它确实有效,现在它已经到位,它使用起来相当透明。我通过Eve添加到MongoDB的对象现在已经嵌入了列表和哈希,但它们也获得了方便的Eve属性,如_created和_updated,而POST和PATCH请求也通过Eve的正常模式得到验证。
唯一真正尴尬的事情是on_insert和on_update发送了稍微不同的参数,所以在我下面的代码中有很多重复,我还没有重构过。
任何字符都可以用作标志:我使用两个下划线来表示最终应该作为单个对象的键/值,以及两个用于应该拆分成列表的符号的&符号。我现在发布的结构如下所示:
"Salary__OtherPay" : "On Application"
"ContactPhone" : "xx",
"JobTypeCodeList" : "Public Sector&&Other",
"CompanyName" : "Scc",
"url" : "xx",
"JobTitle" : "xxx",
"WebAdID" : "TA7494725_1_1",
"JobDescription" : "xxxx",
"JobLocation__DisplayCity" : "BRIDGWATER",
"JobLocation__City" : "BRIDGWATER",
"JobLocation__StateProvince" : "Somerset",
"JobLocation__Country" : "UK",
"JobLocation__PostalCode" : "TA6"
"CustomField1" : "Permanent",
"CustomField3" : "FTJOBUKNCSG",
"WebAdManagerEmail" : "xxxx",
"JobType" : "Full",
"ProductID" : "JCPRI0UK"
我的Eve模式已相应更新,以验证这些新密钥名称的值。然后在后端我定义了下面的函数,它检查传入的键/值并将它们转换为对象/列表,并删除原始__和&&数据:
import re
def flat_to_complex(items=None, orig=None):
if type(items) is dict: # inserts of new objects
if True: # just to force indentation
objects = {} # hash-based container for each object
lists = {} # hash-based container for each list
for key,value in items.items():
has_object_wildcard = re.search(r'^([^_]+)__', key, re.IGNORECASE)
if bool(has_object_wildcard):
objects[has_object_wildcard.group(1)] = None
elif bool(re.search(r'&&', unicode(value))):
lists[key] = str(value).split('&&')
for list_name, this_list in lists.items():
items[list_name] = this_list
for obj_name in objects:
this_obj = {}
for key,value in items.items():
if key.startswith('{s}__'.format(s=obj_name)):
match = re.search(r'__(.+)$', key)
this_obj[match.group(1)] = value
del(items[key])
objects[obj_name] = this_obj
for obj_name, this_obj in objects.items():
items[obj_name] = this_obj
elif type(items) is list: # updates to existing objects
for idx in range(len(items)):
if type(items[idx]) is dict:
objects = {} # hash-based container for each object
lists = {} # hash-based container for each list
for key,value in items[idx].items():
has_object_wildcard = re.search(r'^([^_]+)__', key, re.IGNORECASE)
if bool(has_object_wildcard):
objects[has_object_wildcard.group(1)] = None
elif bool(re.search(r'&&', unicode(value))):
lists[key] = str(value).split('&&')
for list_name, this_list in lists.items():
items[idx][list_name] = this_list
for obj_name in objects:
this_obj = {}
for key,value in items[idx].items():
if key.startswith('{s}__'.format(s=obj_name)):
match = re.search(r'__(.+)$', key)
this_obj[match.group(1)] = value
del(items[idx][key])
objects[obj_name] = this_obj
for obj_name, this_obj in objects.items():
items[idx][obj_name] = this_obj
然后我告诉Eve在该集合的插入和更新上运行该函数:
app.on_insert_myendpoint += flat_to_complex
app.on_update_myendpoint += flat_to_complex
这实现了我所需要的,并且Mongo中的结果记录与上面的问题中的结果相同(使用_created和_updated属性)。它显然不理想,但它实现了它,并且一旦它到位就很容易合作。