Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

220
Views
Populate a Django Model from List of Lists

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!

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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)
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!