当我尝试在django中更新表单时,会遇到这种类型的问题

when i try to update form in django getting this type issue

本文关键字:遇到 种类 问题 类型 表单 django 更新      更新时间:2023-10-27

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

请使用jsondictlist存储在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对象。