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
3 answers

A signal m2m_changed and bug with post_remove

I need to detect a post_remove signal, so I have written : def handler1(sender, instance, action, reverse, model, pk_set, **kwargs): if (action == 'post_remove'): test1() # not declared but make a bug if it works, to detect…
nlassaux
  • 2,335
  • 2
  • 21
  • 35
6
votes
1 answer

Database Modeling with multiple many-to-many relations in Django

I could need some help on designing my models and their relationships. Short overview Book: book titles (e.g. "Lord of the Rings") Tag: tags associated with books (e.g. "Fantasy", "Wizard", "Epic fight") Aspect: aspects associated with tags, or, in…
Alp
  • 29,274
  • 27
  • 120
  • 198
5
votes
2 answers

Post_save received twice for one save even when using dispatch_uid

I have my models in individual files: models \ |__init__.py |event.py |a_thing.py |... In __init__.py I import each model and after that I set the signal handling. For the Event model I need some post_save handling. This is the truncated…
Virgiliu
  • 3,068
  • 6
  • 32
  • 55
5
votes
1 answer

In test cases(unit-testing), Django pre_save signal can not be caught

In Django, my code on catching pre_save signal works well. However, in testcases in tests.py, the signal handler cannot receive anything. Is there any hint for this problem? It seems that my testcases and signal handler are in different apps. Is…
Wei An
  • 1,779
  • 4
  • 13
  • 18
5
votes
0 answers

Trigger Django Signal from Shell

I have configured a signal to run on post_save and its working fine in Django admin and API. But when I try to update the model using mymodel.save() using django shell it does not get triggered. Is there a workaround as I have to manually update…
A Lewis
  • 83
  • 7
5
votes
1 answer

Python Django: Sending a message from server to client on database save()

I want to notify the client when my model is saved. I started by creating a django-signal on post_save. @receiver(post_save, sender=Scooter) async def scooter_post_update(sender, instance, created, **kwargs): # Notify client here Next I created…
5
votes
1 answer

ValueError: Signal receivers must accept keyword arguments (**kwargs). when using Haystack

I am trying to make SignalProcessor as per haystack documentation, here is my code: from haystack.signals import RealtimeSignalProcessor from products.models import ProductCreateModel from django.db import models from star_ratings.models import…
Pankaj Sharma
  • 2,185
  • 2
  • 24
  • 50
5
votes
1 answer

Django save() method not saving

I am using a signal to update fields in the profile table when a new investment is made. Having used a number of print statements inside the update_users_investments function I can see that the function is being called and it is doing the right…
5
votes
1 answer

Django admin not handling ProtectedError exception

I have a Django application that has a model like this: class Foo(models.Model): name = models.CharField(max_length=100) I have a specific instance in this model that it's name is 'bar' (for example) and I want to prevent this instance from…
Navid777
  • 3,591
  • 8
  • 41
  • 69
5
votes
2 answers

Detect field change using post_save instead of pre_save signal

I need to do some actions when one field has changed. Since this action needs to work with already saved object, I can't use pre_save signal like this: @receiver(pre_save, sender=reservation_models.Reservation) def generate_possible_pairs(sender,…
Milano
  • 18,048
  • 37
  • 153
  • 353
5
votes
5 answers

Django rest auth user_logged_in signal

I have a django rest app using django rest auth. I'm trying to log something everytime a user log in using signals. I've searched on the web on how to use signals and I haven't found any interesting material on how to make it work. I think the…
WisdomPill
  • 720
  • 4
  • 11
  • 25
5
votes
2 answers

Django pre_delete signal gets ignored

Here is what is run on pre_delete of model Document. This code gets royally ignored when put in a separate file (signals.py) as suggested best practice. When put in model file, it works fine. from django.db.models.signals import pre_delete,…
Robert Brax
  • 6,508
  • 12
  • 40
  • 69
5
votes
5 answers

How to know when the database is ready in Django?

I need to do stuff as soon as the database is ready in Django. Specifically, I need to perform some calculations on values from db and fill the results into cache. Since django 1.7, the application registry makes it easy to know when an app or…
Thibault J
  • 4,336
  • 33
  • 44
5
votes
1 answer

in django 1.8, how to set sender for post_migrate and post_syncdb signal receiver when a custom user model is set?

Following is my code in the signals.py file placed in the package where the auth model is defined. @receiver(post_migrate, sender=settings.AUTH_USER_MODEL) def define_groups(sender, **kwargs): # Create groups …
anonDuck
  • 1,337
  • 1
  • 9
  • 15
5
votes
1 answer

How to handle Django Singals emitted by multiple models using a common function

I would like to write a common function to handle the post_save signal emitted by Django for multiple models. models.py Class FirstModel(models.Model): pass Class AnotherModel(models.Model): pass Class YetAnotherModel(models.Model): …
Jagadesh Babu T
  • 475
  • 5
  • 13