[Answered ]-Discard older elements in Django queryset

2👍

Django queryset has field lookups.

From the Django docs,

Field lookups are how you specify the meat of an SQL WHERE clause. They’re specified as keyword arguments to the QuerySet methods filter(), exclude() and get().

There are a number of built-in lookups available such as exact, iexact, contains, gt, gte, lt, lte and many more. For more info, please refer to Django queryset field lookups.

To answer your question

dates = Dating.objects.filter((Q(creator=user) | Q(assignee=user)) & Q(accepted=True)) \
                  .exclude(id__in=alreadyRated)

should be

dates = Dating.objects.filter((Q(creator=user) | Q(assignee=user)) & Q(accepted=True)) \
                  .exclude(id__in=alreadyRated, finalDate__lt=datetime.today())

so your function should look like

from datetime import datetime

def get_dates_approved(request):
    user = request.user
    alreadyRated = [x.date.id for x in Ratings.objects.filter(rator=user)]
    dates = Dating.objects.filter((Q(creator=user) | Q(assignee=user)) & Q(accepted=True)) \
                      .exclude(id__in=alreadyRated, finalDate__lt=datetime.today())

Leave a comment