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

203
Views
comparing model A's fields values in model B's field django

I have 100k records in both model 'A' and in model 'B'

Ex:

class A(models.Model):
    user_email = models.EmailField(null=True, blank=True)
    user_mobile = models.CharField(max_length=30, null=True, blank=True)
    book_id = models.CharField(max_length=255, null=True, blank=True)
    payment_gateway_response = JSONField(blank=True, null=True)

class B(models.Model):
    order = models.ForeignKey(A, null=True, blank=True)
    pay_id = models.CharField(max_length=250, null=True, blank=True)
    user_email = models.EmailField(null=True, blank=True)
    user_mobile = models.CharField(max_length=30, null=True, blank=True)
    created = models.DateTimeField(blank=True, null=True)
    total_payment = models.DecimalField(decimal_places=3, max_digits=20, blank=True, null=True)

I want to get B's objects using A's values

for example

all_a = A.objects.all()
for a in all_a:
   b = B.objects.filter(user_email=a.user_email, user_mobile=a.user_mobile)

This is fine, I am getting the results. But as it's 100k records it's taking too much time. for loop iteration is taking time. Is there any faster way to do this in django?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

You can get a list of each value in a and filter b with those values.

a = A.objects.all()
emails = list(a.values_list('user_email', flat=True))
mobiles = list(a.values_list('user_mobile', flat=True))

b = B.objects.filter(user_email__in=emails, user_mobile__in=mobiles)

How ever results may have pair of email and mobile that are not pair in A. But if you make sure that emails and mobiles will be unique in A and the email and mobile in each B are based in one of the A' models, then you won't have any problems.

over 4 years ago · Santiago Trujillo Report

0

If you're not interested in caching the A model, you may have a performance increase using iterator() (see for reference https://docs.djangoproject.com/en/1.11/ref/models/querysets/#iterator):

for a in A.objects.all().iterator():
    b = B.objects.filter(user_email=a.user_email, user_mobile=a.user_mobile)
over 4 years ago · Santiago Trujillo Report

0

You can do

import operator
from django.db.models import Q

q = A.objects.all().values('user_email', 'user_mobile')
B.objects.filter(reduce(operator.or_, [Q(**i) for i in q]))

If you want to do with some operations with every b object depends on a.This is not the way.

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!