我有一个Django models.py文件与定义的类,如下所示:在_subtotal,_Total,和_utilidad字段来自一个相关类使用如何将Python中的NoneType值从聚合转换为float?
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,))
骨料值(CotizacionDetalle)这是一个孩子来自这个类,如在主 - >详细信息的关系,是Cotizacion标题表和CotizacionDetalle Lines表。
当从我的前端跑我没有得到任何错误,都值我需要他们,如图所示如下图:
...但在运行Django管理界面时,我得到以下错误消息:在/管理/ TRANSPORTE/cotizacion/
类型错误
不受支持的操作数类型(个),/: 'NoneType' 和 'INT'
请求方法:GET 请求URL:http://demo.mistenants.com:8000/admin/transporte/cotizacion/ Django的版本:1.9.7 异常类型:类型错误 异常值:
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 admin interfase访问时才得到错误。 Django管理模板对数据类型有更严格的评估吗?我如何使用聚合函数中的变量执行操作?
欢迎堆栈溢出SE。请务必访问https://stackoverflow.stackexchange.com/Tour – SDsolar
@ abijith-mg谢谢,确实更具可读性。 –