DHIS2 Tracker Tables and DHIS2 API

Hi everyone,

May I ask where are tracker values stored in the DHIS2 database. Reason I’m asking is I have created a python program to import data from a excel file into tracker. The imports the data successfully but the data is not showing on the tracker list and I don’t where is it stored in the database. What I know is that the data is there in the database because when I retrieve it, it shows from the IDE console. Here is my python code:

import requests
from requests.auth import HTTPBasicAuth

# Define the DHIS2 server URL and API endpoint for tracker data import
url = 'http://localhost:8081/dhis/api/trackedEntityInstances'
url_1 = 'http://localhost:8081/dhis/api/trackedEntityInstances.json?ou=z1YInlQ16D7'

# Define the headers with authentication and content type information
headers = {
    'Content-Type': 'application/json',
    'Authorization': 'Basic <base64_encoded_username_password>'
}

username = 'username'
password = 'password'

# Define the JSON data for import
json_data = {
    "trackedEntityType": "EWEsyuHlIvA",
    "orgUnit": "z1YInlQ16D7",
    "attributes": [
        {
            "attribute": "Tpu3SL7L2f2",
            "value": "76872520"
        },
        {
            "attribute": "G8ZoPKoXXEl",
            "value": "2006-02-14"
        },
        {
            "attribute": "tG9YczwdloB",
            "value": "Valentincia"
        },
        {
            "attribute": "lfZwUnC1Rkc",
            "value": "Simelane"
        },
        {
            "attribute": "MTZdLwVOS8Y",
            "value": "Phetsile"
        },
        {
            "attribute": "WWpn6CNwthJ",
            "value": "0602142900189"
        },
        {
            "attribute": "MGX7Ly2hKie",
            "value": "Female"
        },
    ],
    "enrollments": [
        {
            "program": "uvAgbwY2pp6",
            "enrollmentDate": "2023-12-01",
            "incidentDate": "2023-12-01",
            "events": [
                {
                    "programStage": "G5Gjke6B0XZ",
                    "eventDate": "2023-12-01",
                    "dataValues": [
                        {
                            "dataElement": "dlwWhI8qBXU",
                            "value": "2023-12-01"
                        },
                        {
                            "dataElement": "UlqRgjTsM5R",
                            "value": "Negative"
                        },
                        {
                            "dataElement": "xyIJ1FJaoKy",
                            "value": "2023-12-01"
                        },
                        {
                            "dataElement": "Z2vNPxaJGVE",
                            "value": "2023-12-10"
                        },
                        {
                            "dataElement": "QAh9nox93hq",
                            "value": "Yes"
                        },
                        # Add more data elements as needed
                    ]
                }
            ]
        }
    ]
}

# Send the POST request to import the tracker data
response = requests.post(url, auth=HTTPBasicAuth(username, password), json=json_data)

# Check the response status
if response.status_code == 200:
    print('Tracker data imported successfully.')

    get_response = requests.get(url_1, auth=HTTPBasicAuth(username, password))

    # Check the response status
    if get_response.status_code == 200:
        tracker_values = get_response.json()
        print(tracker_values)
        for tracker_value in tracker_values['trackerEntityInstances']:
            # Process each tracker value
            print(tracker_value)
    else:
        print('Failed to retrieve tracker values. Status code:', get_response.status_code)
        print('Error message:', get_response.text)
else:
    print('Failed to import tracker data. Status code:', response.status_code)
    print('Error message:', response.text)

Note that just checking the return code doesn’t mean things have been done
you need to look at “import summaries” (and nested as a tree : ex the tracked entity can be created but not the events)

You can verify in the following tables

  • trackedentityinstance : the tei and its attributes
  • programstageinstance : events per stage
1 Like

Thank you let me check that out.