2017-03-17 99 views
0

我有這個錯誤,你能幫我嗎?您需要從「Articulo」模型的「Pedido」 - 「庫存」模型中減去「數量」值,然後保存庫存結果。不支持的操作數類型(s) - =:'str'和'int'

管線

:articulo.stock - = pedido.cantidad

def Update_stock(request, id_pedido, cod_experto): 
if request.method == 'GET': 
    pedido = Pedido.objects.get(id=id_pedido) 
    articulo = Articulo.objects.get(pk=cod_experto) 
    articulo.stock -= pedido.cantidad 
    articulo.save() 
return render(request, 'admindata.html', {'pedido':pedido, 'articulo':articulo}) 

models.py:

class Pedido(models.Model): 
articulo  = models.ForeignKey('Articulo') 
fecha_pedido = models.DateTimeField(auto_now_add=True,null=True, blank=True) 
cantidad  = models.IntegerField(blank=True) 



def __str__(self): 
    return '{}'.format(self.especialidad, self.articulo, self.cantidad, self.estado) 


class Articulo(models.Model): 
cod_experto = models.CharField(max_length=999, primary_key=True, blank=True) 
nombre  = models.CharField(max_length=999, blank=True) 
on_delete=models.CASCADE) 
stock  = models.CharField(max_length=999, blank=True) 
+1

'articulo.stock'是一個字符串,可能。 –

+0

我不能添加任何比Jean-FrançoisFabre所說的更多的東西。它顯然是一個字符串,它在錯誤消息 – WhatsThePoint

+0

中這麼說,非常感謝! –

回答

1

該錯誤消息表示articulo.stock無意中分配STR而不是數字的。

在該模型中,股票被定義爲CharField。可能它應該是一些數字類型,例如IntegerField()

相關問題