Everything works fine until I delete all the objects and try to trigger the url, then it gives me this traceback: list index out of range. I can't use get because there might be more than one object and using [0] with filter leads me to this error when there's no object present, any way around this? I'm trying to get the recently created object of the Ticket model (if created that is) and then perform the logic, so that if the customer doesn't have any tickets, nothing happens but if the customer does then the logic happens
Models
class Ticket(models.Model):
date_posted = models.DateField(auto_now_add=True, blank=True, null=True)
customer = models.ForeignKey(Customer, on_delete=models.SET_NULL, blank=True, null=True)
Views
try:
ticket = Ticket.objects.filter(customer=customer).order_by("-id")[0]
now = datetime.now().date()
set_date = ticket.date_posted
check_time = now - set_date <= timedelta(hours=24)
if check_time:
print('working')
else:
print('not working')
except Ticket.DoesNotExist:
ticket = None
context = {"check_time": check_time}