5

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

6
  • 2
    why can't you try start_date-timedelta(1) ? it will give previous day. Apr 2, 2018 at 9:23
  • What's timezone.now()?
    – CristiFati
    Apr 2, 2018 at 9:24
  • 1
    @CristiFati timezone.now() django's wrapper to datetime which enables timezone functionalities according to configuration of timezone specified in settings.py by default it will follow UTC timezone
    – Gahan
    Apr 2, 2018 at 9:29
  • @VikasDamodar because i didn't know about timedelta HAHAHAAHA THANK YOU Apr 2, 2018 at 9:31
  • @GabbyCervantes So does it resolved after you came to know !!! Apr 2, 2018 at 9:34

2 Answers 2

7

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

0
2
with out date formate

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)

with date formate

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

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.