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
269
votes
11 answers

TransactionManagementError "You can't execute queries until the end of the 'atomic' block" while using signals, but only during Unit Testing

I am getting TransactionManagementError when trying to save a Django User model instance and in its post_save signal, I'm saving some models that have the user as the foreign key. The context and error is pretty similar to this question django…
Gaurav Toshniwal
  • 3,552
  • 2
  • 24
  • 23
114
votes
5 answers

Django signals vs. overriding save method

I'm having trouble wrapping my head around this. Right now I have some models that looks kind of like this: def Review(models.Model) ...fields... overall_score = models.FloatField(blank=True) def Score(models.Model) review =…
imjoevasquez
  • 14,021
  • 6
  • 31
  • 22
98
votes
8 answers

The right place to keep my signals.py file in a Django project

Based on Django's documentation I was reading, it seems like signals.py in the app folder is a good place to start with, but the problem I'm facing is that when I create signals for pre_save and I try to import the class from model it conflicts with…
Mo J. Mughrabi
  • 6,747
  • 16
  • 85
  • 143
92
votes
5 answers

Django post_save() signal implementation

I have a question about django. I have ManyToMany Models here class Product(models.Model): name = models.CharField(max_length=255) price = models.DecimalField(default=0.0, max_digits=9, decimal_places=2) stock =…
haris hamdani
  • 1,367
  • 2
  • 11
  • 18
91
votes
11 answers

django - comparing old and new field value before saving

I have a django model, and I need to compare old and new values of field BEFORE saving. I've tried the save() inheritance, and pre_save signal. It was triggered correctly, but I can't find the list of actually changed fields and can't compare old…
Y.N
  • 4,989
  • 7
  • 34
  • 61
86
votes
8 answers

Identify the changed fields in django post_save signal

I'm using django's post_save signal to execute some statements after saving the model. class Mode(models.Model): name = models.CharField(max_length=5) mode = models.BooleanField() from django.db.models.signals import post_save from…
Wendy
  • 1,523
  • 3
  • 12
  • 16
68
votes
3 answers

What are the options for overriding Django's cascading delete behaviour?

Django models generally handle the ON DELETE CASCADE behaviour quite adequately (in a way that works on databases that don't support it natively.) However, I'm struggling to discover what is the best way to override this behaviour where it is not…
Tom
  • 42,844
  • 35
  • 95
  • 101
62
votes
6 answers

Why Django model signals are not working?

I am trying to create activity streams of users from their status. models: class Status(models.Model): body = models.TextField(max_length=200) image = models.ImageField(blank=True, null=True, upload_to=get_upload_file_name) privacy =…
Kakar
  • 5,354
  • 10
  • 55
  • 93
62
votes
3 answers

Django: how can I tell if the post_save signal triggers on a new object?

I need to do some background post-processing on newly created objects in Django. This post-processing should only run on new objects, not objects that are just updated. I know that in pre_save I can check if the object has an id, if it has not then…
Jon Tirsen
  • 4,750
  • 4
  • 29
  • 27
58
votes
4 answers

How do I prevent fixtures from conflicting with django post_save signal code?

In my application, I want to create entries in certain tables when a new user signs up. For instance, I want to create a userprofile which will then reference their company and some other records for them. I implemented this with a post_save…
poswald
  • 1,755
  • 1
  • 11
  • 11
55
votes
7 answers

How do I mock a django signal handler?

I have a signal_handler connected through a decorator, something like this very simple one: @receiver(post_save, sender=User, dispatch_uid='myfile.signal_handler_post_save_user') def signal_handler_post_save_user(sender, *args,…
StefanoP
  • 3,798
  • 2
  • 19
  • 26
52
votes
3 answers

Django: How to access original (unmodified) instance in post_save signal

I want to do a data denormalization for better performance, and put a sum of votes my blog post receives inside Post model: class Post(models.Model): """ Blog entry """ author = models.ForeignKey(User) title =…
Silver Light
  • 44,202
  • 36
  • 123
  • 164
50
votes
9 answers

Want to disable signals in Django testing

So I have various signals and handlers which are sent across apps. However, when I perform tests / go into 'testing mode', I want these handlers to be disabled. Is there a Django-specific way of disabling signals/handlers when in testing mode? I can…
user2564502
  • 937
  • 2
  • 10
  • 16
48
votes
4 answers

Django accessing ManyToMany fields from post_save signal

I have a Django model and I want to modify the object permissions on or just after save. I have tried a few solutions and the post_save signal seemed the best candidate for what I want to do: class Project(models.Model): title =…
Darwin Tech
  • 18,449
  • 38
  • 112
  • 187
47
votes
9 answers

How to use Django model inheritance with signals?

I have a few model inheritance levels in Django: class WorkAttachment(models.Model): """ Abstract class that holds all fields that are required in each attachment """ work = models.ForeignKey(Work) added =…
Silver Light
  • 44,202
  • 36
  • 123
  • 164
1
2 3
58 59