在 JsonField django 中更新数据
Update data in a JsonField django
我想更新 Django 中 json 字段中的 json 对象,我在更新数据时遇到问题。
我的模型是这样的
https://codeshare.io/Gbeonj
我的 json 看起来像这样
https://codeshare.io/5obDPX
所以基本上 json 有错误的数据,而不是 "NATIONAL ID Card" 它有 "NATIONAL 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": ""
},
"documentType": "NATIONAL ID",
应该是"NATIONAL ID Card"
我正在使用以下脚本更新服务器中的 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_type_value
后,您不会将其设置回 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()
我想更新 Django 中 json 字段中的 json 对象,我在更新数据时遇到问题。
我的模型是这样的 https://codeshare.io/Gbeonj
我的 json 看起来像这样 https://codeshare.io/5obDPX
所以基本上 json 有错误的数据,而不是 "NATIONAL ID Card" 它有 "NATIONAL 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": ""
},
"documentType": "NATIONAL ID",
应该是"NATIONAL ID Card"
我正在使用以下脚本更新服务器中的 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_type_value
后,您不会将其设置回 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()