Setting Academic or Financial Years in Django

て烟熏妆下的殇ゞ 提交于 2021-01-29 11:36:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!