python nonetype转换float_如何在Python中将NoneType值从聚合转换为float?
我有一個Django模型.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,))
在小計、總計和\u utilidad字段中使用的聚合值來自于相關類(cotizaciondelle),該類是該類的子類,如主-->Detail關系中,Cotizacion是頭表,以及cotizaciondelle行表。在
當從我的前端運行時,我沒有得到錯誤,并且值會根據需要顯示,如下圖所示:
…但運行Django管理接口時,我收到以下錯誤消息:
在/admin/transporte/cotizacion/
/:“NoneType”和“int”的操作數類型不受支持
異常位置:C:\Users\adalb\PycharmProjects\tenant\transporte\models.py in utilidad, line 330
330號線是這個:precioRnd = math.ceil(precio / redondeoLps) * redondeoLps
redondeoLps是一個int型變量,值為50(來自外部參數API)
wierd的問題是,我只有在通過Django管理接口訪問時才會得到錯誤。Django管理模板是否對數據類型有更嚴格的評估?如何使用聚合函數中的變量執行操作?在
總結
以上是生活随笔為你收集整理的python nonetype转换float_如何在Python中将NoneType值从聚合转换为float?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python cnn图像分类_关于CNN
- 下一篇: python sftp模块_python