I have a list of lists. I'd like to populate a Model with that data.
my_list = [['January', 'February', 'March'], [10, 20, 30], [2, 4, 6]]
The Model I's like to build is:
MyModel(models.Model):
month = models.Charfield(maxlength=12) # This is my_list[0]
sales = models.DecimalField() # This is my_list[1]
expenses = models.DecimalField() # This is my_list[2]
Please excuse any typos. I typed this out from memory. I am looking for the general principle on how to generate a model from a list of lists. I'd like to avoid storing lists in a single db entry (by using JSON, etc.) and instead would like each db entry to hold a single value. In other words my Month row would have a separate column for January, February, and March.
Thanks in advnace!
You can use zip and bulk_create for creating all records using one query:
objects = []
for month, sales, expenses in zip(*my_list):
objects.append(MyModel(
month=month,
sales=sales,
expenses=expenses
))
MyModel.objects.bulk_create(objects)