Reputation: 51
I'm new to python/django and I have an Events app where users can see upcoming events of an organization but I don't know how to show only future events and disregard events from the past.
models.py
class Event(models.Model):
title = models.CharField(max_length=500)
description = RichTextField()
start_date = models.DateTimeField()
end_date = models.DateTimeField()
created_date = models.DateTimeField(auto_now_add=True)
author = models.ForeignKey(NewUser, on_delete=models.CASCADE)
views.py
class ShowEventsListPageView(ListView):
model = Event
template_name = 'registration/user_events.html'
def get_queryset(self, *args, **kwargs):
user = self.kwargs['pk']
return Event.objects.filter(author=user).annotate(Event.end_date > timezone.now()).order_by('-start_date')
This code is returning '>' not supported between instances of 'DeferredAttribute' and 'datetime.datetime'
Upvotes: 2
Views: 316
Reputation: 32304
The greater than (gt) lookup is used to filter a queryset where a field is greater than a value
Event.objects.filter(
author=user,
end_date__gt=timezone.now()
).order_by('-start_date')
Upvotes: 4