Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

196
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda