【发布时间】:2013-05-30 03:17:08
【问题描述】:
我尝试在视图中按年、月显示信息顺序,如何改进获取它的代码?
提示:我正在编写一份按年和月显示销售订单的报告,帮帮我
谢谢。
views.py
def ventas_mes_anio(request):
ventas = Ventas.objects.filter(Fecha_registro__range=["2011-01-01", "2013-12-31"])
if ventas.is_valid():
enero = Ventas.objects.filter(Fecha_registro__month=1)
febrero = Ventas.objects.filter(Fecha_registro__month=2)
marzo = Ventas.objects.filter(Fecha_registro__month=3)
abril = Ventas.objects.filter(Fecha_registro__month=4)
mayo = Ventas.objects.filter(Fecha_registro__month=5)
junio = Ventas.objects.filter(Fecha_registro__month=6)
julio = Ventas.objects.filter(Fecha_registro__month=7)
agosto = Ventas.objects.filter(Fecha_registro__month=8)
septiembre = Ventas.objects.filter(Fecha_registro__month=9)
octubre = Ventas.objects.filter(Fecha_registro__month=10)
noviembre = Ventas.objects.filter(Fecha_registro__month=11)
diciembre = Ventas.objects.filter(Fecha_registro__month=12)
return render_to_response('ventasxproductosxmes.html',{'datos':ventas,'enero':enero,'febrero':febrero,'marzo':marzo,'abril':abril,'mayo':mayo,'junio':junio,'julio':julio,'agosto':agosto,'septiembre':septiembre,'octubre':octubre,'noviembre':noviembre,'diciembre':diciembre,},context_instance=RequestContext(request))
【问题讨论】:
-
您可以创建使用查询集注释来实现您正在寻找的内容
-
ventas是没有is_valid的QuerySet,可能你的意思是if ventas.exists()或者直接评价if ventas:
标签: python django django-models