I will make the question I asked (How to store a dictionary in a Django database model's field?) better understood:
I got this dictionary
sample_dict = [
{'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}
]
And I would like to store it in the Django DataBase (SQLite3) like this:
BUT before append this dict to the DB I would like to clear the Database and avoid duplicated values (or remove duplicates after append the dict to the db)
If I choose remove duplicates, I should remove duplicates from the "sale_id", "name", "fecha" and "debe" columns, not only from "sale_id" because I got many "sale_id" with the same number but with different dates ("fecha").
I've tried this but every time I run the "objects.create" I got duplicated values in the DB:
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'])
Thanks a lot!