I have a model in Django that represents a week. I'd like to have someone enter any date and have the model automatically save the start of the week. That's easy, just override save.
class Week(models.Model):
start_date = models.DateField()
def save(self):
self.start_date = self.start_date - datetime.timedelta(days=date.weekday())
But I'd also like someone to be able to query any day and get the week. So for example, I'd want someone to do this:
this_week = Week.objects.filter(start_date=date.today())
where today is a Wednesday, and get the week object where the date is set for the start of the week. It needs to work for any date, not just today.
I know we can override get_queryset in a manager, but is there a way to edit what was actually searched for? Every manager example I can find just changed the queryset in a static way. Or would my best bet be trying to subclass the DateField?
(Note code above is typed in, simplified, and may contain mistakes, but it works in my actual code)