Hola a todos, estoy tratando de calcular el precio unitario y la cantidad de esta tabla de la siguiente manera
class Marketers(models.Model): category =models.ForeignKey(Category, on_delete=models.CASCADE, null=True) name =models.CharField(max_length=50, null=True, blank =True) grade =models.CharField(max_length=50, null=True, blank =True) quatity_received = models.IntegerField(default=0, null=True, blank =True) unit_price =models.IntegerField(default=0, null=True, blank =True) customer = models.CharField(max_length=50, null=True, blank =True) date_received = models.DateTimeField(auto_now_add=True) date_sold = models.DateTimeField(auto_now_add=True) @property def get_total(self): total = self.quatity_received * self.unit_price return totalasí es como lo llamo en mi plantilla
<td class="align-middle text-center"> <span class="text-secondary text-xs font-weight-bold">{{ list.get_total }}</span> <p class="text-xs text-secondary mb-0">Overall Price </p> </td>este es el error que estoy recibiendo
TypeError: unsupported operand type(s) for *: 'NoneType' and 'NoneType'por favor necesito ayuda Gracias
Dado que tanto quatity_received como unit_price son anulables, existe la posibilidad de que esté intentando multiplicar dos NoneType juntos, de ahí el error
TypeError: unsupported operand type(s) for *: 'NoneType' and 'NoneType'Ejemplo para recrear el error
[3.8.8] >>> x=None [3.8.8] >>> x [3.8.8] >>> x*1 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'Solución:
Si va a tener campos anulables, intente reescribir la función get_total como tal
@property def get_total(self): if(self.quatity_received is None and self.unit_price is None): return 0 else: return self.quatity_received * self.unit_price