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
6
votes
2 answers

trigger function after returning HttpResponse from django view

I am developing a django webserver on which another machine (with a known IP) can upload a spreadsheet to my webserver. After the spreadsheet has been updated, I want to trigger some processing/validation/analysis on the spreadsheet (which can take…
dino
  • 3,093
  • 4
  • 31
  • 50
6
votes
4 answers

Django signal only works when debug=True, DJANGO 3.2.4

I've been looking everywhere and I coulnd't find any reference about this, my Django model signal only works when the debug=True, but it doesn't work if debug=False, this occur both on localhost and production server. My settings looks like…
Diand
  • 878
  • 7
  • 12
6
votes
4 answers

Django - how do I _not_ dispatch a signal?

I wrote some smart generic counters and managers for my models (to avoid select count queries etc.). Therefore I got some heavy logic going on for post_save. I would like to prevent handling the signal when there's no need to. I guess the perfect…
ohnoes
  • 5,542
  • 6
  • 34
  • 32
6
votes
1 answer

Django pre_save triggered twice

I am using django signals for data denormalization. Here is my code: # vote was saved @receiver(pre_save, sender=Vote) def update_post_votes_on_save(sender, instance, **kwargs): """ Update post rating """ # is vote is being updated, then we…
Silver Light
  • 44,202
  • 36
  • 123
  • 164
6
votes
3 answers

Access to related data of newly created model instance using post_save signal handler

I need to send an e-mail when new instance of Entry model is created via admin panel. So in models.py I have: class Entry(models.Model): attachments = models.ManyToManyField(to=Attachment, blank=True) #some other fields #... sent…
Dzejkob
  • 2,302
  • 2
  • 15
  • 20
6
votes
2 answers

Django: Signal for "post initial migration"

For setting up new development systems I would like to have a "post initial migration" signal. It seems that something like this does not exist yet. In detail: I want a signal which runs after the initial migrations have run. The second call to…
guettli
  • 25,042
  • 81
  • 346
  • 663
6
votes
2 answers

Is it possible to selectively suppress a post_save (or other) signal in Django?

I'm wondering whether it's possible to selectively suppress a Django signal (such as post_save or post_init) on object creation, or, alternatively, send it certain parameters. What I have is a User object, which can be created in many different ways…
Herman Schaaf
  • 46,821
  • 21
  • 100
  • 139
6
votes
1 answer

Django signals for processing a simple user notification system

I'm learning to code and have a live Django project to keep me motivated. In my Django app, users leave comments, while others reply to the said comments. Everytime a user refreshes their home page, I calculate whether they've received any new…
Hassan Baig
  • 15,055
  • 27
  • 102
  • 205
6
votes
3 answers

Celery + Django Signals

I am trying to leverage the post_save function of Django Signals in combination with Celery tasks. After a new Message object is saved to the database, I want to evaluate if the instance has one of two attributes and if it does, call the…
Joe Fusaro
  • 847
  • 2
  • 11
  • 23
6
votes
2 answers

Django signals doesn't work

My models.py:> class Aval(models.Model): cliente = models.ForeignKey(Cliente) salao = models.ForeignKey(Salao) rate = models.IntegerField(choices=RATE, default=5) criacao = models.DateTimeField(blank=True, null=True, auto_now=True) …
rayashi
  • 1,721
  • 3
  • 20
  • 28
6
votes
2 answers

Django REST Framework: return 404 (not 400) on POST if related field does not exist?

I'm developing a REST API which takes POST requests from some really brain-dead software which can't PATCH or anything else. The POSTs are to update Model objects which already exist in the database. Specifically, I'm POSTing data for objects with a…
6
votes
2 answers

post_save signal on m2m field

I have a pretty generic Article model, with m2m relation to Tag model. I want to keep count of each tag usage, i think the best way would be to denormalise count field on Tag model and update it each time Article being saved. How can i accomplish…
Dmitry Shevchenko
  • 31,814
  • 10
  • 56
  • 62
6
votes
2 answers

Django pre_save signal - would an exception fail the transaction?

I want to do some custom actions before a user is created. and I thought of using the pre_save signal for that. And in case one of those action would raise an exception stop the transaction, abort creating the user etc. Is this the way to go? would…
alonisser
  • 11,542
  • 21
  • 85
  • 139
6
votes
2 answers

django Signals not working as expected

I am trying to create a project for creating feeds/activity feeds of a user with the help of a blog. These are the models - class StreamItem(models.Model): user = models.ForeignKey(User) content_type = models.ForeignKey(ContentType) …
Aamu
  • 3,431
  • 7
  • 39
  • 61
6
votes
2 answers

Django Many-to-Many relation insertion control

I have the following models: class Item(models.Model): # fields # ... class Collection(models.Model): items = models.ManyToManyField(Item, related_name="collections") # other fields # ... Now I want two things: I want to…
Constantinius
  • 34,183
  • 8
  • 77
  • 85