日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python nonetype转换float_如何在Python中将NoneType值从聚合转换为float?

發布時間:2025/3/19 python 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。