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

327
Views
Convert ManyToMany into ForeignKey (django)

I have a ManyToMany field in my database that I want to convert to a ForeignKey relationship. The relationships are already one-to-many, so there will be no pigeonholing.

The closest question I can find on stackoverflow is this more complicated situation in a different framework/language

My simplified django models are shown below. The fields in question already exist in the database, and we just need to populate the DbLocation.pattern field.

class DbPattern(models.Model):
    locations = models.ManyToMany(DbLocation) #trying to remove this
    ...

class DbLocation(models.Model)
    pattern = models.ForeignKey(DbPattern) #and replace it with this
    ...

My naive solution is a nested for-loop. It works, but looks like it will take days to handle several million records:

patterns = DbPattern.objects.all()
for p in patterns:
    locs = p.locations # there ary many locations
    for l in locs:
        l.pattern = p # each location has exactly 1 pattern.

Is there an easy way to implement this in either Python/Django or PostreSQL that will run fast? I imagine there is a way to do this via queries. I only need to do it once.

Thanks in advance for your help!

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I managed to get several orders of magnitude speedup with a few simple tweaks. More speedup could probably be achieved, but this is sufficient for my purposes.

Naive Code (1000 samples take 793 seconds)

patterns = DbPattern.objects.all()
for p in patterns:
    locs = p.locations # there ary many locations
    for l in locs:
        l.pattern = p # each location has exactly 1 pattern.
        l.save()

First improvement (1000 sample takes 11 seconds)

patterns = DbPattern.objects.all()
for p in patterns:
    locs = p.locations
    locs.update(pattern=p)

Second improvement (1000 sample takes 2.5 seconds)

patterns = DbPattern.objects.all()
[p.locations.all().update(pattern=pattern) for p in patterns]
over 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!