Django:在 queryset.update 上发出信号
如果您使用 queryset.delete()
方法,Django 会发送 pre/post_delete 信号,但不应该在 queryset.update()
上发送 pre/post_save 信号>?
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()
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许应该如此,但事实并非如此。
.update()
不会对 QuerySet 中的各个对象调用.save()
方法,而是在单个 SQL 调用中更新所有对象(UPDATE,因为它发生)。由于它不使用.save()
,因此调用保存前和保存后信号会不一致。我当然可以想象人们可能希望它这样做的用例,但我也可以想象人们不希望这样做的情况。在我看来,不调用保存前和保存后信号是正确的行为,因为它为程序员留下了更多的灵活性。手动触发这些信号并不难,我认为要求程序员记住触发信号以获得所需的行为绝对是一个更好的设计决策,而不是要求他们记住断开信号以避免不良行为。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.我同意,
.update()
可用于防止信号调度,例如,如果您想更新实例而不触发信号,您应该使用它而不是.save()< /代码> 方法。
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.