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
5
votes
1 answer

Using a pre_save while specifying update_fields

I have a pre_save defined on MyModel which looks something like this: @receiver(pre_save, sender=MyModel) def _mymodel_pre_save(sender, instance, **kwargs): if some_condition(): instance.somecolumn = 'eggs' i.e. it expects to be able to…
Josh Kupershmidt
  • 2,540
  • 21
  • 30
5
votes
2 answers

Prevent signal sending for specific save() calls

I'm performing some persistence actions with the model's object when receiving a post_save signal, which includes a call to save(). Obviously the save() call sends a post_save signal again and I'm landing in a signal recursion. Is there a way to…
Manuel Faux
  • 2,317
  • 5
  • 24
  • 35
5
votes
3 answers

Django manytomany signals?

Let's say I have such model class Event(models.Model) users_count = models.IntegerField(default=0) users = models.ManyToManyField(User) How would you recommend to update users_count value if Event add/delete some users ?
Djangonaut
  • 5,511
  • 7
  • 40
  • 53
4
votes
1 answer

Dynamic upload path - include originating field

I have a Django model with multiple ImageFields and use a callable to determine the upload path. I want to include the originating upload field's name in the upload path, in this case tiny, small, medium or press. The only way I could think of was…
jmagnusson
  • 5,799
  • 4
  • 43
  • 38
4
votes
1 answer

Why is my SIGNAL not working in Django - what I'm doing wrong?

first of all, I'm not a developer. Trying to build an Application with Django (Version 3.1.2) but facing some issues with signals. I have this Models in my models.py: class PhoneNumbers(models.Model): number = models.CharField(_('Category'),…
sokolata
  • 491
  • 3
  • 7
  • 21
4
votes
4 answers

Why aren't signals simply called events?

From what I can tell, in Python and and Django, signals are simply delegated events. Is there anything that functionally differentiates them from the typical notion of events in C#, Java, ActionScript, etc?
Soviut
  • 88,194
  • 49
  • 192
  • 260
4
votes
1 answer

Django signals not working when placed outside models.py

I'm trying to print some text after Django model in an app has been saved. I have created a signal for that in a signals.py file in the same application. However, it's not working as expected (i.e., the function is not being called and text is not…
Underoos
  • 4,708
  • 8
  • 42
  • 85
4
votes
1 answer

Django change the order of pre_save post_save signals being called when using inlines?

I have an Order (1) and OrderLine (n) model, here order can have multiple order-lines. This is all run from inside the Django-admin, where the OrderLine is setup as part of the inlines on OrderAdmin(admin.ModelAdmin). Simplified like this: class…
Roger
  • 7,535
  • 5
  • 41
  • 63
4
votes
1 answer

Working with Django's post_save() signal

I have two tables: class Advertisement(models.Model): created_at = models.DateTimeField(auto_now_add=True) author_email = models.EmailField() class Verification(models.Model): advertisement = models.ForeignKeyField(Advertisement) …
mktums
  • 426
  • 1
  • 4
  • 15
4
votes
1 answer

Django signals vs channels

I have a Django-based project that I would like to make real-time so I thought of Django channels. However, I am still not sure whether this is the right project to apply Django channels and which part of the project I should apply it. I have a…
Amoroso
  • 945
  • 2
  • 10
  • 21
4
votes
1 answer

Django pre_save signal and ModelAdmin custom error message

I have a model whose pre_save() signal is connected to a remove service (json, REST, etc.) in the following way: before saving locally, query the remote service, asking for remote insertion remote service does its things, the main being checking…
Hal
  • 537
  • 4
  • 13
4
votes
1 answer

Django signals How to perform post_save method with multiple senders and multiple instances?

I have two models Profile and Company models.py class Profile(models.Model): user = models.OneToOneField(User) company = models.ForeignKey('company.Company', null=True) phone = models.CharField(max_length=10,…
Kishan M
  • 173
  • 2
  • 13
4
votes
0 answers

Django Rest Framework models post_save signal is executed after post request is completed

I have the following serializer: class CreateOrderSerializer(OrderSerializer): class Meta(MetaOrder): fields = MetaOrder.fields + ('pair',) read_only_fields = MetaOrder.read_only_fields def create(self, validated_data): …
Oleg Belousov
  • 9,981
  • 14
  • 72
  • 127
4
votes
1 answer

Showing validation error from m2m_changed signal in django admin

I am trying to validate that the variable foo is the same for MyModel and Item before adding it as a m2m. I want to raise a ValidationError in the admin if it is not. models.py class Item(models.Model): foo = models.CharField(max_length=200) …
Maxim
  • 507
  • 1
  • 7
  • 18
4
votes
1 answer

Assigning user permissions with post_save signal

I have a custom User model called EmailUser and I'm trying to assign a custom model permission on post_save. I have the following signal in models.py @receiver(post_save, sender=EmailUser) def assign_permissions(sender, **kwargs): emailUser =…
oppositeday
  • 113
  • 1
  • 5