Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: Signal on queryset.update

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()?

like image 927
Bernhard Vallant Avatar asked Nov 07 '09 14:11

Bernhard Vallant


People also ask

Where is Pre_save signal in Django?

pre_save. This is sent at the beginning of a model's save() method. Arguments sent with this signal: sender.

How do I register a signal in Django?

To receive a signal, register a receiver function using the Signal. connect() method. The receiver function is called when the signal is sent. All of the signal's receiver functions are called one at a time, in the order they were registered.

What is the use of the Post_delete signal in Django?

There are 3 types of signal. pre_save/post_save: This signal works before/after the method save(). pre_delete/post_delete: This signal works before after delete a model's instance (method delete()) this signal is thrown.

What is created in Django signals?

Django provides many built-in signals dispatchers like – pre_save, post_save, pre_delete, post_delete, m2m_changed, request_started and request_finished. Let's say you want to check an email id before entering it in the model, but there are several places in your codebase that this model can be created or updated.


1 Answers

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.

like image 72
Josh Ourisman Avatar answered Sep 26 '22 07:09

Josh Ourisman