当我尝试在django中更新表单时,会遇到这种类型的问题
when i try to update form in django getting this type issue
i将此类型值存储在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"
]
}
如果我不更新此代码并获取数据库并尝试执行。我没有犯任何错误。当我尝试在更新后执行此代码时。我得到以下定义错误:
追踪(最近一次通话):
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
请使用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,即obj-
并且在检索时使用
json.loads
所以你会得到原来的样子,你之前保存在数据库中。。
:)
存储时使用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,simplejson返回字节字符串(它返回否则为unicode对象),而json始终返回unicode对象。
相关文章:
- 将PHP变量传递给jQuery时遇到问题
- 我的jQuery插件参数没有正确启动,遇到了问题
- 在AJAX回调和呈现PIXIJS之间处理程序流时遇到了问题
- 将jQuery转换为用户脚本的普通Javascript时遇到问题
- 将子文档推送到父数组时遇到问题
- JQuery FullCalendar在从ajax成功调用rerenderEvents时遇到问题
- regex来构建字符串,直到遇到第N个匹配项
- 遇到错误后,更改检测似乎中断
- 在将javascript附加到我的html中时遇到问题
- 遇到400错误请求(Angular+WebAPI)的问题
- 在使用javascript的Ajax方面遇到了困难
- 为什么我在以编程方式打开窗口时遇到问题
- 在将缩略图链接到模态时遇到问题
- 在我的网站上创建一个在1-10之间不断变化的数字时遇到了问题.Javascript
- 使用 ng 模型获取数据时遇到问题
- 是否有其他人在设计Twitter Typeahead时遇到问题'的搜索栏
- 使用jquery进行整数比较时遇到问题
- 使用PhantomJS下载动态web内容时遇到问题
- 设置RequireJS时遇到问题
- 让Javascript在SharePoint 2010上运行时遇到问题