I want to store this list of dictionarys in a Django database:
h = [
{'sale_id': 14,
'name': 'Macarena',
'fecha': datetime.date(2021, 3, 11),
'debe': 500.0},
{'sale_id': 14,
'name': 'Macarena',
'fecha': datetime.date(2021, 4, 11),
'debe': 500.0},
{'sale_id': 15,
'name': 'Yamila',
'fecha': datetime.date(2021, 4, 14),
'debe': 2000.0}
]
I've tried this:
h = tabla_creditos()
class Creditos1(models.Model):
sale_id = models.IntegerField(default=0)
name = models.CharField(max_length=150)
fecha = models.DateTimeField(default=datetime.now)
debe = models.IntegerField(default=0)
for i in range(0,len(h)):
Creditos1.objects.create(name=h[i]['name'], sale_id=h[i]['sale_id'], fecha=h[i]['fecha'], debe=h[i]['debe'])
where "h" is that dictionary, which results from "tabla_creditos()" function. And then, I tried to create the values for the DB with "objects.create" but I got duplicated values stored in DB:
So, how can I store that dict in the DB?
I found this solution: How to store a dictionary in a Django database model's field but none of the answers helped me.
Thanks!
EDIT With the Rohith answer, I got this:
But I would like something like this, transform every key of the dict in a DB column: