我将此类型值存储在DB中:
输入:
{
"PatientProfile__is_recruiter": "1",
"PatientProfile__partner": "FMCS",
"PatientProfile__health_insurance_provider": "MILITARY/VA",
"PatientProfile__has_medical_home": "0",
"PatientProfile__medical_history_heart_disease": "0",
"PatientProfile__medical_history_hypertension": "0",
"data_model_name": [
"PatientProfile"
]
}当我尝试更新和更新之后,我发现相同的结果如下:
{
"PatientProfile__is_recruiter": "1",
"PatientProfile__partner": "FMCS",
"PatientProfile__health_insurance_provider": "MILITARY/VA",
"PatientProfile__has_medical_home": "0",
"PatientProfile__medical_history_heart_disease": "0",
"PatientProfile__medical_history_hypertension": "0",
"data_model_name": [
"PatientProfile"
]
}如果我没有更新此代码并将其提取到db并尝试执行,请执行。我没有任何错误。当我试图在更新后执行此代码时。下面是定义错误:
回溯(最近一次调用):
File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py", line 111, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/contrib/auth/decorators.py", line 23, in _wrapped_view
return view_func(request, *args, **kwargs)
File "/home/ubuntu/django-apps/project_name/../project_name/apps/accounts/decorators.py", line 44, in inner_decorator
return func(request, *args, **kwargs)
File "/home/ubuntu/django-apps/project_name/../project_name/apps/reports/views.py", line 97, in hiv_report_new
return form.get_itable(pk)
File "/home/ubuntu/django-apps/project_name/../project_name/apps/reports/forms.py", line 454, in get_itable
custom_data = ast.literal_eval(report_qs[0]['query'])
File "/usr/lib/python2.6/ast.py", line 49, in literal_eval
node_or_string = parse(node_or_string, mode='eval')
File "/usr/lib/python2.6/ast.py", line 37, in parse
return compile(expr, filename, mode, PyCF_ONLY_AST)
File "<unknown>", line 1
{
^
SyntaxError: invalid syntax发布于 2013-11-27 10:59:05
请使用json将dict或list存储在db中
例如:
存贮
obj = json.dumps("{
'PatientProfile__is_recruiter': '1',
'PatientProfile__partner': 'FMCS',
'PatientProfile__health_insurance_provider': 'MILITARY/VA',
'PatientProfile__has_medical_home': '0',
'PatientProfile__medical_history_heart_disease': '0',
'PatientProfile__medical_history_hypertension': '0',
'data_model_name': [
'PatientProfile'
]
}")并存储json obj i.
在检索使用时
json.loads因此,您将得到原来保存在db..。
:)
发布于 2013-11-27 12:27:03
在存储时使用json.dumps:
obj = json.dumps("{
'PatientProfile__is_recruiter': '1',
'PatientProfile__partner': 'FMCS',
'PatientProfile__health_insurance_provider': 'MILITARY/VA',
'PatientProfile__has_medical_home': '0',
'PatientProfile__medical_history_heart_disease': '0',
'PatientProfile__medical_history_hypertension': '0',
'data_model_name': ['PatientProfile']
}")检索时,您有两个选项,即;
示例:
>>>simplejson.loads('{"x":"y"}')
{'x': 'y'}
>>> json.loads('{"x":"y"}')
{u'x': u'y'} 也就是说,如果字符串是ASCII (否则返回unicode对象),simplejson返回字节字符串,而json总是返回unicode对象。
https://stackoverflow.com/questions/20240311
复制相似问题