I have made a Website for the Debating Society of our College using Django
.
I would like to have 2 types of events Past and Upcoming (Future) according to the date_of_competition, i.e., if the date and time of competition is past current date and time then return it in past events, and if the date and time of competition is in future of the current date and time then return it in future events
Here are my views.py
file and models.py
file for events
models.py
:
from django.db import models
class Format(models.Model):
format_name = models.CharField(max_length=100, null=False, unique=True)
def __str__(self):
return self.format_name
class Organiser(models.Model):
organiser_name = models.CharField(max_length=140, null=False, unique=True)
def __str__(self):
return self.organiser_name
class Event(models.Model):
banner_image = models.ImageField(upload_to="events")
event_name = models.CharField(max_length=150, null=False)
organiser_of_event = models.ForeignKey(Organiser, on_delete=models.CASCADE)
format_of_event = models.ForeignKey(Format, on_delete=models.CASCADE)
date_of_event = models.DateTimeField(auto_now_add=False)
registration_fees = models.IntegerField(default=0, help_text="Enter Registration Fees For The Event in Rupees")
details = models.TextField(null=True, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.event_name
view.py
:
from django.shortcuts import render
from .models import Event
# Create your views here.
def events(request):
context = {
'title': 'Events',
'events': Event.objects.all()
}
return render(request, 'main/Events.html', context)
What logic should be written in order to get both future and past events from my events table?
(If there's anything you don't understand or need something extra, feel free to ask for it).
CodePudding user response:
The obvious way is to just do two queries, using the __gte
(greater than or equal) and __lt
(less than) operators.
from django.utils import timezone
# ...
now = timezone.now()
context = {
'title': 'Events',
'future_events': Event.objects.filter(date_of_event__gte=now),
'past_events': Event.objects.filter(date_of_event__lt=now),
}
You could also do a single query and do the filtering in Python:
now = timezone.now()
all_events = Event.objects.all()
future_events = [e for e in all_events if e.date_of_event >= now]
past_events = [e for e in all_events if e.date_of_event < now]
context = {
'title': 'Events',
'future_events': future_events,
'past_events': past_events,
}
CodePudding user response:
Another option would be to overwrite the manager you use for events so you could have Event.past_events.all() and Event.future_events.all()
You could do that with the following:
from django.utils import timezone
Class PastEventManager(models.Manager):
def get_queryset(self):
today = timezone.now()
return super().get_queryset().filter(date_of_event__lt=today)
Class Event(models.Model):
...
past_events = PastEventManager()
future_events = FutureEventManager() # same but with date_of_event__gte=today
You can use it in your code like the following:
past_events = Event.past_events.all()
future_events = Event.future_events.filter(put_your_query)