[Fixed]-Django: correctly retrieve data where date and time are greater than now

27👍

Use Q objects, which allow you to make queries with OR statements.

from django.db.models import Q
Events = Event.objects.filter(
    Q(date=now.date(),
    time__gte=now.time()
    |Q(date__gt=now.date())
).order_by('-date')

Note that you might want to sort on the time field as well:

order_by('-date', '-time')

2👍

What i did when i had a similar problem:
My Problem is to find Upcoming events and Past events from my event section and i sloved like this…
<—#Problemsolving #Datefiltering #Django #Backend —>

@api_view(["GET"])
@check_mode## Heading ##
def past_events(request):
    profile_data = get_current_profile(request)
    profile_id = profile_data["user_profile_data"]["user_profile_pk"]
    today = date.today()

    if Event.objects.filter(event_user=profile_id,end_date__lt=today).exists():
        instances = Event.objects.filter(event_user=profile_id,end_date__lt=today)

        serialized_data = EventSerializer(
            instances, 
            context = {
                "request": request
            },
            many = True
        ).data

        response_data = {
            "Statuscode":6000,
            "data" : serialized_data,
            "title": "Past event"
            }
    else:
        response_data = {
            "StatusCode" :6001,
            "title" : "Failed",
            "message" : "Events not found" 
        }
    return Response(response_data, status=status.HTTP_200_OK)

@api_view(["GET"])
@check_mode
def upcoming_events(request):
    profile_data = get_current_profile(request)
    profile_id = profile_data["user_profile_data"]["user_profile_pk"]
    today = date.today()

    

    if Event.objects.filter(event_user=profile_id,start_date__gt=today).exists():
        instances = Event.objects.filter(event_user=profile_id,start_date__gt=today)

        serialized_data = EventSerializer(
            instances, 
            context = {
                "request": request
            },
            many = True
        ).data

        response_data = {
            "Statuscode":6000,
            "data" : serialized_data,
            "title": "Upcoming event"
            }
    else:
        response_data = {
            "StatusCode" :6001,
            "title" : "Failed",
            "message" : "Events not found" 
        }
    return Response(response_data, status=status.HTTP_200_OK)

Leave a comment