Search code examples
pythondjangotimezonedjango-timezone

How do I get 1 day before timezone.now()


I have a button that should appear 1 day before the model object's start_date. In my view I have a functions that tells me if the object's start_date or end_date is now.

    def has_engagement(self):
        from rental.models import Inquiry
        inquiries = Inquiry.objects.filter(inquiryequipment__equipment=self).filter(start_date__lte= timezone.now()).\
            filter(end_date__gte=timezone.now()).filter(status="CO")
        if not inquiries:
            return False
            # no current confirmed engagement
        else:
            return True
            # there is a current confirmed engagement

instead of this, i need to check if today is a day before the start or end dates. Any ideas? Thanks


Solution

  • you can use timedelta as below:

    previous = timezone.now() - timedelta(1)  # returns date object of previous day
    next = timezone.now() + timedelta(1)  # returns date object of next day
    Inquiry.objects.filter(start_date__lte=previous, end_date__gte=next, status="CO", inquiryequipment__equipment=self)
    

    Note you can filter queryset and queries using comma seperated