首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当我试图更新django中的表单时,会出现这种类型的问题

当我试图更新django中的表单时,会出现这种类型的问题
EN

Stack Overflow用户
提问于 2013-11-27 10:47:28
回答 2查看 52关注 0票数 0

我将此类型值存储在DB中:

输入:

代码语言:javascript
复制
{
    "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"
    ]
}

当我尝试更新和更新之后,我发现相同的结果如下:

代码语言:javascript
复制
{
    "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并尝试执行,请执行。我没有任何错误。当我试图在更新后执行此代码时。下面是定义错误:

回溯(最近一次调用):

代码语言:javascript
复制
  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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-11-27 10:59:05

请使用jsondictlist存储在db

例如:

存贮

代码语言:javascript
复制
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.

在检索使用时

代码语言:javascript
复制
json.loads

因此,您将得到原来保存在db..。

:)

票数 1
EN

Stack Overflow用户

发布于 2013-11-27 12:27:03

在存储时使用json.dumps:

代码语言:javascript
复制
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']
}")

检索时,您有两个选项,即;

示例:

代码语言:javascript
复制
>>>simplejson.loads('{"x":"y"}') 
{'x': 'y'}


>>> json.loads('{"x":"y"}') 
{u'x': u'y'} 

也就是说,如果字符串是ASCII (否则返回unicode对象),simplejson返回字节字符串,而json总是返回unicode对象。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20240311

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档