Questions tagged [django-signals]

Django signals allow listeners to be registered for events within the framework. This allows decoupled handling of, for example, model deletions.

Django signals allow listeners to be registered for events within the framework. This allows decoupled handling of, for example, model deletions.

Quote from docs:

Django includes a “signal dispatcher” which helps allow decoupled applications get notified when actions occur elsewhere in the framework. In a nutshell, signals allow certain senders to notify a set of receivers that some action has taken place. They’re especially useful when many pieces of code may be interested in the same events.

See documentation.

877 questions
24
votes
2 answers

Django: Obtaining the absolute URL without access to a request object

I have a model like the one below. When an instance is created, I want to send out an e-mail to an interested party: class TrainStop(models.Model): name = models.CharField(max_length=32) notify_email = models.EmailField(null=True,…
Belmin Fernandez
  • 8,307
  • 9
  • 51
  • 75
24
votes
1 answer

Are django signals also included inside of the transaction.atomic decorator?

I have a model file that uses a post_save signal to create a linked row in another table. In typical fashion, I can create a page from one of my views which is decorated with @transaction.atomic. I would like to know if this decorator will put the…
Sam Texas
  • 1,245
  • 14
  • 30
24
votes
2 answers

Why does Django's signal handling use weak references for callbacks by default?

The Django docs say this on the subject: Note also that Django stores signal handlers as weak references by default, so if your handler is a local function, it may be garbage collected. To prevent this, pass weak=False when you call the…
Jason C
  • 21,377
  • 10
  • 38
  • 33
23
votes
3 answers

post_save signal isn't called

I've already read all related questions. I have two Django projects, and signals work fine in one, but do not work in second one (I've just copy-pasted code and changed names respectively). I have an orders app with Order model. App is included in…
artem
  • 16,382
  • 34
  • 113
  • 189
22
votes
4 answers

How To Run Arbitrary Code After Django is "Fully Loaded"

I need to perform some fairly simple tasks after my Django environment has been "fully loaded". More specifically I need to do things like Signal.disconnect() some Django Signals that are setup by my third party library by default and connect my own…
Chris W.
  • 37,583
  • 36
  • 99
  • 136
22
votes
1 answer

How can I have a Django signal call a model method?

Maybe it's just late, but I cannot figure out why this isn't working. When I have a post_save signal call a generic function, it works, but when I have a post_save signal call a method from a model, nothing happens. Here is code that works: class…
winduptoy
  • 5,366
  • 11
  • 49
  • 67
21
votes
1 answer

handle `post_save` signal in celery

I have a rather long running task that needs to be executed after inserting or updating an specific model. I decided to use post_save signal instead of overriding save method to reduce coupling. Since Django signals are not asynchronous I had to do…
Mohammad Jafar Mashhadi
  • 4,102
  • 3
  • 29
  • 49
18
votes
4 answers

Save the related objects before the actual object being edited on django admin

Is it possible to save the related objects before the actual object being edited on a django admin form? For example: in models.py class Parent(model.Model): pass class Child(model.Model): parent =…
Calin Don
  • 865
  • 2
  • 11
  • 19
17
votes
2 answers

Django - When should I use signals and when should I override save method?

Personally I like using signals: from django.db import models from django.db.models.signals import pre_save class MyModel(models.Model): ... def custom_action_before_saving(sender, instance, *args, **kwargs): …
Gocht
  • 9,924
  • 3
  • 42
  • 81
17
votes
2 answers

How can I send signals from within Django migrations?

I use Django 1.7 migrations, and in particular, want to populate a newly-created database with initial data. Thus, I use a data migration for this. It looks like this: def populate_with_initial_data(apps, schema_editor): User =…
Torsten Bronger
  • 9,899
  • 7
  • 34
  • 41
17
votes
1 answer

Using Django's m2m_changed to modify what is being saved pre_add

I am not very familiar with Django's signals and could use some help. How do I modified the pk_set before the instance is saved? Do I have to return something to the signal caller (like the kwargs)? Or do I save the instance myself? As a…
thornomad
  • 6,707
  • 10
  • 53
  • 78
17
votes
2 answers

Django signal emitting once, received twice -- Why?

I'm working with Django signals, but they seem to be received twice, even if emitted once. Here's the code I'm working with (it's a simple wrapper to use Uploadify with Django)... # Signal-emitting code... emits whenever a file upload is received #…
T. Stone
  • 19,209
  • 15
  • 69
  • 97
17
votes
3 answers

Django - Check diference between old and new value when overriding save method

thanks for your time. I'm on Django 1.4, and I have the following code: Its the overriden save method for my Quest model. @commit_on_success def save(self, *args, **kwargs): from ib.quest.models.quest_status_update import QuestStatusUpdate …
Francisco
  • 1,352
  • 4
  • 13
  • 27
16
votes
4 answers

Why can I access an object during it's post_save Signal, but not when I trigger code within that signal that calls it on another process

All, I've got a issue with django signals. I have a model In an effort to speed up responsiveness of page loads, I'm offloading some intensive processing that must be done, via a call to a second localhost webserver we're running, both using the…
mklauber
  • 1,126
  • 10
  • 19
16
votes
2 answers

Django error: []

I have done the following signal in my project: @receiver(pre_save, sender=group1) @disable_for_loaddata def total_closing_group1(sender,instance,*args,**kwargs): total_group_closing_deb_po =…
Niladry Kar
  • 1,163
  • 4
  • 20
  • 50
1 2
3
58 59