Python script for updating rows in a table

Sharing python script that gets all rows in a table and then sets division field value to IT for rows that have John Doe value in user field.

This script can be modified for various other cases.

Replace placeholders with your token and table id.

To run this snippet you'll need to install requests module. Probably easiest way would be to install python and pip (installation steps depend on your OS), then run pip install requests and then run the script itself - python filename.py.

import requests

TOKEN = "YOUR_TOKEN_HERE"
TABLE = "YOUR_TABLE_ID_HERE"

response = requests.get(
    f"https://data-api.blaze.today/api/database/rows/table/{TABLE}/?user_field_names=true",
    headers={
        "Authorization": f"Token {TOKEN}"
    }
)

response_json = response.json()
rows = response_json["results"]

for row in rows:
    if row["user"] == "John Doe":
        row_id = row["id"]
        row_name = row["user"]
        response = requests.patch(
            f"https://data-api.blaze.today/api/database/rows/table/{TABLE}/{row_id}/?user_field_names=true",
            headers={
                "Authorization": f"Token {TOKEN}",
                "Content-Type": "application/json"
            },
            json={
                "division": "IT"
            }
        )
        if response.status_code == 200:
          print(f"Updated row '{row_id}' with name '{row_name}'")
        else:
          print(f"Failed to update row '{row_id}' with name '{row_name}'")
2 Likes