I am working on a Django project where I want to get all Event Tickets that the Event Date has not passed. That is, I have an event table with some fields amongst them is date and every Ticket is tied to an event, and I want a situation where I can display a List of Tickets that their Events has not passed the current date. For example if a Birthday Event has a date of 12/09/2022 and a Ticket has been created on this event then by today (13/09/2022) this Event should not be listed for Tickets to be generated.
I have tried to figure it out but couldn't get it working the way I explained above, may be I am missing out something. Here is my Models code:
class Event(models.Model):
event_name = models.CharField(max_length=100)
date = models.DateField(auto_now_add=False, auto_now=False, null=False)
event_venue = models.CharField(max_length=200)
event_logo = models.ImageField(default='avatar.jpg', blank=False, null=False, upload_to ='profile_images')
added_date = models.DateTimeField(auto_now_add=True)
def __str__(self):
return f"{self.event_name}"
#Prepare the url path for the Model
def get_absolute_url(self):
return reverse("event_detail", args=[str(self.id)])
class Ticket(models.Model):
event = models.ForeignKey(Event, on_delete=models.CASCADE)
price = models.PositiveIntegerField()
category = models.CharField(max_length=100, choices=PASS, default=None, blank=True, null=True)
added_date = models.DateField(auto_now_add=True)
def __str__(self):
return f"{self.event} "
#Prepare the url path for the Model
def get_absolute_url(self):
return reverse("ticket-detail", args=[str(self.id)])
Here is my views code:
def Ticket_list(request):
tickets = Ticket.objects.filter(event__date__lte=datetime.today())
context = {
'tickets':tickets,
}
return render(request, 'user/ticket_list.html', context)
Your answer shall highly be appreciated. thanks