Relative new to Django / python. I have a API call request and i got JSON data from it:
[{'node': 'node01', 'cpu': 0.0193098516002146, 'mem': 92328939520, 'status': 'online'}, {'node': 'node02', 'cpu': 0.0100098516002146, 'mem': 72823919520, 'status': 'online'}, {'node': 'node03', 'cpu': 0.0023498516002146, 'mem': 92328939520, 'status': 'online'}]
If i make i more human-readable:
[{'node': 'node01', 'cpu': 0.0193098516002146, 'mem': 92328939520, 'status': 'online'}, {'node': 'node02', 'cpu': 0.0100098516002146, 'mem': 72823919520, 'status': 'online'}, {'node': 'node03', 'cpu': 0.0023498516002146, 'mem': 92328939520, 'status': 'online'}]
I have the following model:
class Node(models.Model):
node = models.CharField(max_length=2000, null=True, blank=True)
cpu = models.FloatField(null=True, blank=True)
mem = models.FloatField(null=True, blank=True)
status = models.CharField(max_length=2000, null=True, blank=True)
Now i want to save the API data into the model, so in this case 3 "records" in the DB.
I tried with the following:
for node in r:
a = Node(
cpu=node['cpu'],
mem=node['mem'],
node=node['node'],
status=node['status'],
)
a.save()
r is the API call data. But this saves only the last API data (node03). Also the records have to be updated when i do the API call again (to reflect mem changes etc)