2013-03-03 95 views
0

我已经更改了保存方法以包含修改日期字段以在所有保存中进行更改。我想知道是否使用update()方法更新模型。保存方法会被调用吗?Django自定义保存方法

请回答,如果不解释比我怎么能对所有的修改

回答

2

如果read the documentation on the update method of a queryset你会发现它更新修改日期字段说以下内容:

最后,实现更新()不在SQL级别更新,因此不会在模型上调用任何save()方法,也不会发出pre_save或post_save信号(这是调用Model.save()的结果)。

如果您想更新一堆记录,有一个自定义的保存()方法的模型,环比他们并调用保存(),像这样:

for e in Entry.objects.filter(pub_date__year=2010): 
    e.comments_on = False 
    # Or in your case, update the date here 
    e.save() 
+0

对!此外,请记住存在[auto_now_add](https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.DateField.auto_now_add),还有[model util MonitorField]( https://github.com/carljm/django-model-utils#monitorfield)。两种方法都可以避免覆盖保存方法。 – danihp 2013-03-03 15:57:08