0

我正在尝试获取已售产品的总数,这是代码:

模型.py

class Ventas(models.Model):
  codigoventa = models.CharField(max_length=7, unique=True)
  codigocliente = models.ForeignKey('Clientes')
  Fecha_registro = models.DateTimeField("Fecha de edicion", auto_now_add=True) 
  totalventa = models.DecimalField(max_digits=10, decimal_places = 2)
  codigosucursal = models.ForeignKey('Sucursales')
  totalventa = models.IntegerField()

  def __unicode__(self):
      return self.codigoventa

视图.py

 def ventas_anio(request):
        ventas = Ventas.objects.all().aggregate(total_payment=Sum('totalventa'))

        return render_to_response('ventasanual.html',{'datos':ventas}, context_instance=RequestContext(request))

我无法使用此视图。

4

1 回答 1

0

ventas = Ventas.objects.aggregate(total_payment=Sum('totalventa')) 如果您在各自的数据库中有数据,我认为它应该可以完美运行。输出将是一个字典。如{'total_payment': Decimal('xx.xx')} 格式。

def ventas_anio(请求):

ventas = Ventas.objects.all().aggregate(total_payment=Sum('totalventa'))
sum_ventas = ventas['total_payment']
return render_to_response('ventasanual.html',{'datos':sum_ventas}, context_instance=RequestContext(request))
于 2013-06-01T04:24:48.533 回答