当我尝试更新django中的表单时遇到此类问题

时间:2013-11-27 10:47:28

标签: javascript php python django

我将此类型值存储在DB:

INPUT:

{
    "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

2 个答案:

答案 0 :(得分:1)

请使用jsondict

中存储listdb

例如。

存储时

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

所以你会得到它之前保存在db ..

中的内容

:)

答案 1 :(得分:0)

存储时使用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返回bytestrings(它返回 否则为unicode对象),而json总是返回unicode对象。