如何将日期范围的Mysql查询转换为Django查询

时间:2014-11-13 11:23:40

标签: python mysql django

python 新手以及django也是,

问题

我需要在Django中为日期范围编写一个查询

我知道mysql查询

SELECT * 
FROM productreport 
WHERE start_date BETWEEN '2014-10-12 00:00:00' and '2014-11-12 00:00:00' 
OR end_date BETWEEN '2014-10-12 00:00:00' and '2014-11-12 00:00:00';

我需要使用filter(**kwargs)

动态更改上述查询

被修改

def performance(request):
    daterangestart = str(request.POST['daterangepicker_start'])
    datereangeend = str(request.POST['daterangepicker_end'])
    kwargs['start_date__range'] =[str(daterangestart), str(datereangeend)]
    kwargs['end_date__range'] =[str(daterangestart), str(datereangeend)]
    reportDetails = productreport.objects.filter(**kwargs)

1 个答案:

答案 0 :(得分:0)

看看Q objects

from django.db.models import Q

date_filter = Q(start_date__range = [str(daterangestart), str(datereangeend)] )
date_filter |= Q(end_date__range = [str(daterangestart), str(datereangeend)] )
reportDetails = productreport.objects.filter(date_filter)