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

192
Visualizações
Django QuerySet: filter by the value of another field

I have a model I wish to filter by its attribute a. The model also has another attribute b. I am trying to filter entries where a is 0 or a has the value of the b attribute (for each row, obviously). How can I filter by the value of another column?

Here is what I have tried, and the missing piece:

MyModel.objects.filter(Q(a=0) | Q(a=???)) # ??? is to be the value of the `b` column

I am using Django 1.4 and I know it is an old version that is no longer supported but unfortunately performing the upgrade is not up to me.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

I think you can use an F expression with your Q object:

MyModel.objects.filter(Q(a=0) | Q(a=F('b')))

I'll also suggest you schedule your upgrade now, otherwise, you'll be missing out on other features and security fixes.

over 4 years ago · Santiago Trujillo Relatório

0

You can use the F model for comparing two fields.

from django.db.models import F
MyModel.objects.filter(a__exact = F('b'))

So for your joint query you can use something like,

MyModel.objects.filter(Q(a=0) | Q(a__exact=F('b')))
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