Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save a model without sending a signal?

How can I save a model, such that signals arent sent. (post_save and pre_save)

like image 764
agiliq Avatar asked Oct 12 '09 14:10

agiliq


People also ask

How do you override the Save method of a model?

save() method from its parent class is to be overridden so we use super keyword. slugify is a function that converts any string into a slug. so we are converting the title to form a slug basically.

How do I save models in Django?

Creating objects To create an object, instantiate it using keyword arguments to the model class, then call save() to save it to the database. This performs an INSERT SQL statement behind the scenes. Django doesn't hit the database until you explicitly call save() . The save() method has no return value.

What is pre save signal in Django?

pre_save) is provoked just before the model save() method is called, or you could say model save method is called only after pre_save is called and done its job free of errors.

What is the use of the Post_delete signal in Django?

Django Signals - post_delete()To notify another part of the application after the delete event of an object happens, you can use the post_delete signal.


2 Answers

It's a bit of a hack, but you can do something like this:

use a unique identifier with a filter and then use the update method of the queryset (which does not trigger the signals)

user_id = 142187 User.objects.filter(id=user_id).update(name='tom') 
like image 78
Jiaaro Avatar answered Oct 09 '22 06:10

Jiaaro


This ticket has been marked as "wontfix" because:

In short, it sounds like, given the defined purpose of signals, it is the attached signal handler that needs to become more intelligent (like in davedash's suggestion), rather than the code that emits the signal. Disabling signals is just a quick fix that will work when you know exactly what handlers are attached to a signal, and it hides the underlying problem by putting the fix in the wrong place.

like image 35
Jake Avatar answered Oct 09 '22 05:10

Jake