Year Based Filtering of a Django Model

I’m working on a Django app for a client so their staff can more easily submit vacation requests and track their amount used/remaining. Because their time off resets at the end of the calendar year, I needed a good way to filter requests by year. I ended up creating a custom Model manager (with some tips from my friend Ben) that would automatically filter to the current year, yet allow filtering by a given year.

class YearManager(models.Manager):
    def year(self, year=None):
        year = year or
        return super(YearManager, self).get_queryset().filter(start_date__year=year)

This lets me get the current year’s items with MyModel.objects.year() or a given year with MyModel.objects.year(year=2021).

Tony Scida @tonyskyday