POST DataElements in Python (Django)

Hi All

I am trying to created Data elements in DHIS2.32 via Web API using the code below. Unfortunately i am getting ERROR 500

    for value in storedMetaFields.values(): 
        url = 'http://x.x.x.x:8080/api/dataElements'
        headers = {'Content-type': 'application/json'}
        shortName = value

        if len(shortName) > 50:
            print('ShortName Too Long:'+ " "+shortName)  
            shortName = shortName[0:49]                                       


        dataElement = {
        'aggregationType' : 'SUM',
        'domainType': 'TRACKER',
        'categoryCombo': 'bjDvmb4bfup', 
        'valueType': 'TEXT',
        'zeroIsSignificant': 'true',
        'name': shortName,
        'shortName':shortName
        }

        r = requests.post(url, data=json.dumps(dataElement), headers=headers,auth=HTTPBasicAuth(settings.DHIS2_USER, settings.DHIS2_PASSWORD))

Many thanks

Getting the following error in Postman

{
“httpStatus”: “Internal Server Error”,
“httpStatusCode”: 500,
“status”: “ERROR”,
“message”: “Cannot construct instance of org.hisp.dhis.category.CategoryCombo (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value (‘bjDvmb4bfuf’)\n at [Source: (org.apache.catalina.connector.CoyoteInputStream); line: 4, column: 34] (through reference chain: org.hisp.dhis.dataelement.DataElement["categoryCombo"])”
}

Removing the ‘categoryCombo’: ‘bjDvmb4bfup’, did the trick

dataElement = {
“aggregationType” : “SUM”,
“domainType”: “TRACKER”,
“valueType”: “TEXT”,
“zeroIsSignificant”: “true”,
“name”: shortName,
“shortName”:shortName
}

Hi,

I am building a django module for the openimis initiative,

it is based on dhis2.py and I took and expand models based on pydantic I found on the dhis2 repos

Basically it enable you to work with django queryset paginator and pydantic enforce some contrainst on the fields (I have still to improve that part to ensure the text fields are not too long)

the only specific data is in services.py, views.py and the convertors

if it is useful for other I could check how to publish a generic version on pip

br

1 Like