Django: Signal on queryset.update
Asked Answered
G

2

12

Django is sending the pre/post_delete signals if you are using the queryset.delete() method, but shouldn't it then also send pre/post_save on queryset.update()?

Godoy answered 7/11, 2009 at 14:12 Comment(0)
B
14

Perhaps it should, but it doesn't. .update() does not call the .save() method on the individual objects in the QuerySet, and instead updates the all in a single SQL call (UPDATE, as it happens). Since it doesn't use .save(), it would be inconsistent for it to call the pre- and post-save signals. I can certainly envision use-cases in which one might want it to do so, but I can also envision cases in which one wouldn't. It seems to me that not calling the pre- and post-save signals is the correct behavior here as it leaves more flexibility for the programmer. It's not hard to trigger those signals manually, and I think it's definitely a better design decision to ask programmers to remember to trigger the signals to get desired behavior than asking them to remember to disconnect the signals to avoid undesired behavior.

Biology answered 7/11, 2009 at 17:12 Comment(2)
Well though the reasons mentioned I think this is somehow inconsistent behaviour, because the queryset.delete() method also doesnt call delete() on the single instances, but it send the same signals as model.delete() does!Godoy
Also, in order to be able to send a save-related signals on a queryset, they'd essentially need to do a select for the elements in addition to the update, eliminating the benefits of having a bulk update method.Gorcock
A
1

I agree, .update() can be used to prevent a signal dispatch, for example if you wanna update an instance without firing a signal you should use it and not the .save() method.

Aspergillum answered 20/5, 2022 at 13:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.