如何在Python中将NoneType值从aggregate转换为float?

时间:2017-04-17 04:46:29

标签: python django aggregate-functions type-conversion

我有一个Django models.py 文件,其类定义如下:

class Cotizacion(models.Model):
    # Fields
    nombre = models.CharField(max_length=100)
    slug = extension_fields.AutoSlugField(populate_from='nombre', blank=True)
    fecha_vence = models.DateField(default=now)
    _subtotal = models.DecimalField(max_digits=10,
                                    decimal_places=2,
                                    null=True,
                                    blank=True,
                                    db_column='subtotal',
                                    default=0)
    _total = models.DecimalField(max_digits=10,
                                 decimal_places=2,
                                 null=True,
                                 blank=True,
                                 db_column='total',
                                 default=0)
    _utilidad = models.DecimalField(max_digits=8,
                                    decimal_places=6,
                                    null=True,
                                    blank=True,
                                    db_column='utilidad',
                                    default=0)
    # utilidad = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True)
    # total = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True)
    creado = models.DateTimeField(auto_now_add=True, editable=False)
    actualizado = models.DateTimeField(auto_now=True, editable=False)

    # Relationship Fields
    itinerario = models.ForeignKey(Itinerario, on_delete=CASCADE, verbose_name='itinerario')
    nivel_de_precio = models.ForeignKey(NivelDePrecio,
                                        verbose_name='nivel de precio',
                                        on_delete=models.PROTECT, null=True)

    class Meta:
        ordering = ('-id',)
        verbose_name = _('Cotización')
        verbose_name_plural = _('Cotizaciones')

    def __str__(self):
        return self.nombre

    def __unicode__(self):
        return u'%s' % self.nombre

    @property
    def subtotal(self):
        agregado = self.lineas.aggregate(subtotal=Sum('monto'))
        return agregado['subtotal']

    @subtotal.setter
    def subtotal(self, value):
        self._subtotal = value

    @property
    def total(self):
        agregado = self.lineas.aggregate(total=Sum('total'))
        return format(math.ceil(agregado['total'] / redondeoLps) * redondeoLps, '.2f')
        # return math.ceil(agregado['total'] / redondeoLps) * redondeoLps

    @total.setter
    def total(self, value):
        self._total = value

    @property
    def utilidad(self):
        agregado1 = self.lineas.aggregate(costo=Sum('monto'))
        agregado2 = self.lineas.aggregate(precio=Sum('total'))
        precio = agregado2['precio']
        precioRnd = math.ceil(precio / redondeoLps) * redondeoLps
        if agregado2['precio'] == 0:
            return 0
        else:
            ganancia = round((precioRnd - agregado1['costo']) / precioRnd, 4)
            return ganancia

    @utilidad.setter
    def utilidad(self, value):
        self._utilidad = value

    def get_absolute_url(self):
        return reverse('transporte_cotizacion_detail', args=(self.slug,))

    def get_update_url(self):
        return reverse('transporte_cotizacion_update', args=(self.slug,))

_subtotal _total _utilidad 字段中使用的汇总值来自相关类别( CotizacionDetalle )这是一个来自这个班级的孩子,如 Master - >细节关系, Cotizacion Header表, CotizacionDetalle Lines表。

当从我的前端运行时,我没有得到错误,并且在我需要时显示值,如下图所示:

note the 'Precio' value

...但是在运行Django管理界面时,我收到以下错误消息:

/ admin / transporte / cotizacion /

中的TypeError

/:' NoneType'不支持的操作数类型和' int'

请求方法:GET 请求网址:http://demo.mistenants.com:8000/admin/transporte/cotizacion/ Django版本:1.9.7 异常类型:TypeError 例外值:

unsupported operand type(s) for /: 'NoneType' and 'int'

例外地点:

C:\Users\adalb\PycharmProjects\tenant\transporte\models.py in utilidad, line 330 

第330行就是这个:

precioRnd = math.ceil(precio / redondeoLps) * redondeoLps

redondeoLps 是一个int类型变量,值为50(来自外部参数API)

奇怪的是,我只能通过Django管理界面访问时收到错误。 Django管理模板是否对数据类型有更严格的评估?如何使用聚合函数的变量执行操作?

1 个答案:

答案 0 :(得分:0)

你可以做到

from django.db.models.functions import Coalesce
self.lineas.aggregate(precio=Coalesce(Sum('total'), 0))

precioRnd = math.ceil((precio or 0) / (redondeoLps or 0)) * redondeoLps