Skip to content Skip to sidebar Skip to footer

Django Orm Conditional Filter Like Case When Then

I'm using Django 1.11, Postgresql 9.2, python 3.4 I want to select data based on table's column named event_type if event type is single then compare date that should be of same d

Solution 1:

You can combine many Q objects with () signs. I your case I suppose this will work:

single_events = crm_models.EventsMeta.objects.filter(
    (Q(event_type="single") & Q(repeat_start=today)) |
    (Q(event_type="recurring") & Q(repeat_start__lte=today))
)

Solution 2:

use this:-

today = datetime.date.today().strftime('%Y-%m-%d')
single_events = crm_models.EventsMeta.objects.filter(
    event_type__in = ["single", "recurring"]
    repeat_start=today
)

Post a Comment for "Django Orm Conditional Filter Like Case When Then"