[Django]-Django Filter based on 2 fields

3👍

You can work with Coalesce [Django-doc] to determine what field to use:

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
)

or prior to with .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
)

Leave a comment