我尝试在查询中过滤日期和月份,但无法按月过滤。
我检查了我的代码,但我无法在我的代码中找到错误,请帮助我。
我在下面的代码中遗漏了什么吗?
class CustomerVisits(AdminOnlyMixin, ListView):
model = Visit
template_name = "affiliates/admin/visits/list.html"
paginate_by = 10
context_object_name = "visits"
def get_affiliate_user(self):
affiliate = Affiliate.objects.get(affiliate_user=self.request.user)
return affiliate
def get_queryset(self):
object_list = self.model.objects.all()
email = self.request.GET.get('email') # test@gmail.com
date = self.request.GET.get('monthYear')# January 2016
if email:
object_list = object_list.filter(affiliate_user__affiliate_user__email=email)
if date:
month, year = date.split()
month_num = {
'Jan' : 1,
'Feb' : 2,
'Mar' : 3,
'Apr' : 4,
'May' : 5,
'Jun' : 6,
'Jul' : 7,
'Aug' : 8,
'Sep' : 9,
'Oct' : 10,
'Nov' : 11,
'Dec' : 12
}[month[:3]]
object_list = object_list.filter(visit_time__year=year,visit_time__month=month_num)
print object_list
return object_list
答案 0 :(得分:1)
您必须在数据库中安装时区定义,才能在USE_TZ = True时使用此过滤器。
您可以使用mysql_tzinfo_to_sql
进行安装sudo mysql_tzinfo_to_sql /usr/share/zoneinfo/ | mysql -u root mysql
这是我的detailed answer