I am trying to filter a query by weeks. It will return jobs if the jobs report_by_date is within a week.
Job.objects.filter((report_by_date-datetime.today()).days <= 7)
This can be done via the datetime. combine method as above, but I've found that it can be a bit simpler to merely accommodate the discrepancy by adding a timedelta(days=1) to either the start/end date in the range -- depending on the problem. So Example.
Django offers a QuerySet method called select_related() that allows you to retrieve related objects for one-to-many relationships. This translates to a single, more complex QuerySet, but you avoid additional queries when accessing the related objects. The select_related method is for ForeignKey and OneToOne fields.
You can create a datetime for one week ago, then filter all jobs after that.
from datetime import datetime, timedelta
one_week_ago = datetime.today() - timedelta(days=7)
jobs = Job.objects.filter(report_by_date__gte=one_week_ago)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With