1

I want to update a json object that is in a jsonfield in django, i am having a problem updating the data.

My model looks like this https://codeshare.io/Gbeonj

My json looks like this https://codeshare.io/5obDPX

so basicly the json has wrong data , instead of "NATIONAL ID Card" it has "NATIONAL ID" so i want to update this json object to have the right data. here is what am talking about

"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": ""
      }, 

the "documentType": "NATIONAL ID", should be "NATIONAL ID Card"

i am using the following script to update the json object in the server.

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()

The problem is that this object is not been updated kindly what am i doing wrong?

1 Answer 1

2

Once you have validated the doc_type_value you are not setting it back into the upload object, you need to update the upload object:

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.