django' int'对象没有属性' save'

时间:2015-11-26 03:43:18

标签: python django

我是django的新手,我收到以下代码的错误,我猜它很简单,但我真的无法理解。

from django.db.models.signals import post_save
from django.dispatch import receiver

from myapp.models import MyModel1, MyModel2


@receiver(post_save, sender=MyModel1)
def test_signal(sender, **kwargs):
    #import pdb;pdb.set_trace();
    if kwargs['instance']:
        instance = kwargs['instance']

        if instance.old_processed_value == False and instance.processed == True:
            var1 = MyModel2.objects.get(val1= instance.val1, val2 = instance.val2)
            var2 = var1.quantity+instance.qty_change
            var2.save()

2 个答案:

答案 0 :(得分:1)

我认为应该是:

var1.quantity += instance.qty_change
var1.save()

答案 1 :(得分:0)

var1 = MyModel2.objects.get(val1= instance.val1, val2 = instance.val2)

var1是MyModel2对象,错误名称

var1有save()方法,但var2是一个int valua,所以发生错误

你想做什么:

model2_obj = MyModel2.objects.get(val1= instance.val1, val2 = instance.val2)
model2_obj.quantity+instance.qty_change
model2_obj.save()