Save API response.json() values to database

Django 5.1.1
Database MariaDB 11.1

NOTE: The code below is partial

Statement:
I have never saved data to a database that was not coming from a form. In this case I need to iterate thru response.json() and save the value(s) as strings from the key:value pairs to MariaDB table as defined below. That’s it.
I have researched Django docs and the forum and nothing addressing this.

Goal:
Save key value data from response.json() api get to database. Later I will use that data in an html table.

The api get request works fine. It returns data as requested (see ‘FOR TEST PURPOSES’ code below)

api_code_inc.py


def apitest_inc(request):
  url = (tenant_url + business_object_url)

  response = requests.request("GET", url, headers=headers, data=payload, params=params, timeout=3)
  try:
      response.raise_for_status()
            
      r_dict = response.json() 
      
      if response.status_code == 200:                                                                     
              print(response.status_code, "Ok, Valid Response")
              
              # FOR TEST PURPOSES - Prints the data returned from the request as expected. 
              print(json.dumps(r_dict, indent=4, sort_keys=True))       
              
              # FOR TEST PURPOSES - The following works as expected.
              for val in r_dict['value']:
                    
                    RecId_values = (val['RecId'])
                    print('RecId             : ', RecId_values)
                    
                    CreatedDateTime_values = (val['CreatedDateTime'])
                    print('CreatedDateTime   : ', CreatedDateTime_values)
                
                    IncidentNumber_values = (val['IncidentNumber']) 
                    print('IncidentNumber    : ', IncidentNumber_values)
                    

models.py

class Ivanti_incs(models.Model):                                                    
      RecId = models.CharField(max_length=50, blank=True, null=True)
      CreatedDateTime = models.CharField(max_length=20, blank=True, null=True)
      IncidentNumber = models.CharField(max_length=10, blank=True, null=True)

def __str__(self):
            return f"{self.IncidentNumber}'s message"

Assuming these are all new entries (not updating existing entries), what you’re wanting to do here is create new instances of the models where these fields have the supplied values. (See the create() method for one way to do this.)

The Playing with the API section in part 2 shows examples of creating instances of models from data.

Whether these data values are coming from an HTML form or JSON doesn’t matter. The process of creating the model instances doesn’t change.

Another option would be to create a model form for this incoming data and bind each instance to the form from the JSON dict. (That would give you the ability to use validators on that data.)

If there is a chance that you are updating existing rows, then the update_or_create function may be helpful there.