I am trying to call this overrride save method in models:
我试图在模型中调用这个覆盖保存方法:
def save(self, *args, **kwargs):
if self.done is True:
if self.averagepa is None:
pass
elif self.averagepa < 26:
self.links = 5
elif self.averagepa < 31:
self.links = 10
elif self.averagepa < 36:
self.links = 15
elif self.averagepa < 41:
self.links = 20
else:
self.links = 99
super(KW, self).save(*args, **kwargs)
This works perfectly if I just save model in admin panel. But when I try to update it via ./manage.py shell
like this:
如果我只是在管理面板中保存模型,这非常有效。但是,当我尝试通过./manage.py shell更新它时,如下所示:
KW.objects.filter(id=138).update()
It doesn't trigger it. How can I call save override method with update from shell?
它不会触发它。如何从shell调用保存覆盖方法?
1 个解决方案
#1
11
This is the documented behaviour of the update()
method.
这是update()方法的记录行为。
Be aware that the
update()
method is converted directly to an SQL statement. It is a bulk operation for direct updates. It doesn’t run anysave()
methods on your models, or emit the pre_save or post_save signals (which are a consequence of callingsave()
), or honor the auto_now field option. If you want to save every item in a QuerySet and make sure that thesave()
method is called on each instance, you don’t need any special function to handle that. Just loop over them and callsave()
.请注意,update()方法直接转换为SQL语句。这是直接更新的批量操作。它不会在模型上运行任何save()方法,也不会发出pre_save或post_save信号(这是调用save()的结果),或者遵循auto_now字段选项。如果要保存QuerySet中的每个项目并确保在每个实例上调用save()方法,则不需要任何特殊函数来处理它。只需循环遍历它们并调用save()。
In your case:
在你的情况下:
kw = KW.objects.get(id=138)
# update kw
kw.save()
#1
11
This is the documented behaviour of the update()
method.
这是update()方法的记录行为。
Be aware that the
update()
method is converted directly to an SQL statement. It is a bulk operation for direct updates. It doesn’t run anysave()
methods on your models, or emit the pre_save or post_save signals (which are a consequence of callingsave()
), or honor the auto_now field option. If you want to save every item in a QuerySet and make sure that thesave()
method is called on each instance, you don’t need any special function to handle that. Just loop over them and callsave()
.请注意,update()方法直接转换为SQL语句。这是直接更新的批量操作。它不会在模型上运行任何save()方法,也不会发出pre_save或post_save信号(这是调用save()的结果),或者遵循auto_now字段选项。如果要保存QuerySet中的每个项目并确保在每个实例上调用save()方法,则不需要任何特殊函数来处理它。只需循环遍历它们并调用save()。
In your case:
在你的情况下:
kw = KW.objects.get(id=138)
# update kw
kw.save()