问题
This may seem rather insignificant, but how do you set a financial or academic year, say 2018/19 or 2017/2018 in a Django model and reference the current financial year for querying, in this case 2019/20
回答1:
consider using a custom queryset
class CustomQuerySet(models.QuerySet):
def current_financial_year(self):
return self.filter(datefield__gte=current_finyear_start, datefield__lte=current_finyear_end)
then make a manager out of it
CustomManager = models.Manager.from_queryset(CustomQueryset)
# Then in your model:
class MyModel(models.Model):
objects = CustomManager()
#use it like this:
MyModel.objects.current_financial_year().filter(...)
MyModel.objects.current_financial_year().get(...)
check https://docs.djangoproject.com/en/dev/topics/db/managers/#creating-a-manager-with-queryset-methods
来源:https://stackoverflow.com/questions/56644983/setting-academic-or-financial-years-in-django