Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 更新JsonField django中的数据_Python_Json_Django_Django Jsonfield - Fatal编程技术网

Python 更新JsonField django中的数据

Python 更新JsonField django中的数据,python,json,django,django-jsonfield,Python,Json,Django,Django Jsonfield,我想更新django中json字段中的json对象,更新数据时遇到问题 我的模型看起来像这样 我的json看起来像这样 所以基本上json有错误的数据,而不是“国家ID卡”,它有“国家ID”,所以我想更新这个json对象以获得正确的数据。 这就是我所说的 "info": { "mobilePhone": "", "firstName": "david", "tags": [], "middleName": "mirale"

我想更新django中json字段中的json对象,更新数据时遇到问题

我的模型看起来像这样

我的json看起来像这样

所以基本上json有错误的数据,而不是“国家ID卡”,它有“国家ID”,所以我想更新这个json对象以获得正确的数据。 这就是我所说的

"info": {
        "mobilePhone": "", 
        "firstName": "david", 
        "tags": [], 
        "middleName": "mirale", 
        "gender": "Male", 
        "documentType": "NATIONAL ID", 
        "beneficiary": false, 
        "dateOfBirth": "1995-03-04T08:01:42.165Z", 
        "documentNumber": "519011016721", 
        "dateOfBirthExact": false, 
        "role": "Child", 
        "lastName": "ABSURG0058", 
        "recipient": "Alternate", 
        "homePhone": ""
      }, 
“文件类型”:“国民身份证”
应为“国民身份证”

我正在使用以下脚本更新服务器中的json对象

import django
django.setup()

import sys
reload(sys)    # to re-enable sys.setdefaultencoding()
sys.setdefaultencoding('utf-8')


import json
from django.db import transaction
from maidea.apps.mobile.models import MobileDocument


office = 'sa_de'

#we fetch all the mobile documents from that have failed
uploads = MobileDocument.objects.filter(
    status=MobileDocument.ERROR,
    mobile_upload__office__slug=office
)

print('Number of uploads fetched: %d' %(uploads.count()))

with transaction.atomic():
    for upload in uploads:
        for member in upload.data['members']:
            try:
                doc_type_value = member['info'].get('documentType')
            except:
                doc_type_value = None
            if doc_type_value == 'NATIONAL ID':
          doc_type_value = doc_type_value.replace('NATIONAL ID', 'NATIONAL ID Card')
          assert doc_type_value == 'NATIONAL ID Card'
                  upload.save()

问题是此对象未被更新,我做错了什么?

一旦您验证了
doc\u type\u值
您没有将其设置回
upload
对象,您需要更新
upload
对象:

for upload in uploads:
    data = upload.data
    updated_members = []
    for member in data['members']:
        try:
            doc_type_value = member['info'].get('documentType')
        except KeyError:
            pass
        else:
            if doc_type_value == 'NATIONAL ID':
                doc_type_value = 'NATIONAL ID Card'
                member['info']['documentType'] = doc_type_value
        updated_members.append(member)

    data['members'] = updated_members
    upload.data = data
    upload.save()