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)
答案 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)