2017-04-17 10 views
0

私は次のように定義されたクラスとジャンゴmodels.pyファイル有する:_subtotalに使用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,)) 

集計値、 _Total、及び_utilidadフィールドが関連するクラスから来る(をこのクラスの子であるCotizacionDetalle)とマスター - >詳細の関係であり、コタイサシオンヘッダーテーブルとCotizacionDetalleラインテーブル。私のフロントエンドから実行

私はエラーを取得していないと私はそれらを必要として、次の図に示すような値は、提示されている:

note the 'Precio' value

...しかし、Djangoの管理インタフェースIを実行するとき'NoneType' と 'int型

01:/ための管理/ TRANSPORTE/cotizacion/

サポートされていないオペランドのタイプ(S)/で

はTypeError:次のエラーメッセージが表示されます

リクエスト方法: リクエストURLを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はこの1つです:

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

redondeoLpsは、値が50のint型変数です(外部パラメータAPIから)

私はDjangoの管理インターフェイスでアクセスするとエラーになります。 Djangoの管理用テンプレートはデータ型のより厳密な評価をしていますか?集計関数の変数を使用して操作を実行するにはどうすればよいですか?

+0

ようこそを行うことができます。 https://stackoverflow.stackexchange.com/Tour – SDsolar

+0

@ abijith-mgでツアーを行ってください。本当にありがとうございます。 –

答えて

0

あなたは、スタックオーバーフローSEに

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

OR

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

ありがとう、それは解決策でした。 –

+0

最初のオプションは問題を解決しましたが、2番目のオプションは結果を0 –

関連する問題