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

118
Views
Filtro Django basado en 2 campos

Tengo un modelo donde puedo configurar expire_date_1 y expire_date_2. Y 2 filtros como este.

 filter_1 = Q(expire_date_1__isnull=False) & Q(expire_date_1__lte=after_30days) filter_2 = Q(expire_date_2__isnull=False) & Q(expire_date_2__lte=after_30days)

Quiero filtrar el modelo que si expire_date_2 no es nulo, entonces use filter_2; de lo contrario, use filter_1 Intenté hacerlo con Case y When pero no puedo filtrar en una función When, ¿puedo?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Puede trabajar con Coalesce [Django-doc] para determinar qué campo usar:

 from django.db.models import Coalesce MyModel.objects.alias( exp_date= Coalesce('expire_date_2', 'expire_date_1') ).filter( exp_date__lte=after_30_days )

o anterior a django-3.2 con .annotate(…) :

 from django.db.models import Coalesce MyModel.objects. annotate( exp_date=Coalesce('expire_date_2', 'expire_date_1') ) .filter( exp_date__lte=after_30_days )
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!