Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 version of __init__.py:

from django.db.models.signals import post_save
from django.dispatch import receiver

from core.models.event import Event

# Event
@receiver(post_save, sender = Event)
def event_post_save(sender, dispatch_uid = 'nope', **kwargs):
    print kwargs.get('created')
    print '------'

Whenever I save an Event via the console the message in the post_save is printed once but whenever I use the admin interface it gets printed twice. This may be because I import the models inside admin.py as well.

Is there a workaround for this so that I can save Event objects from the admin interface without the post_save firing twice?

like image 965
Virgiliu Avatar asked Oct 10 '22 01:10

Virgiliu


1 Answers

It's probably from Django/Python import silliness. You need dispatch_uid like you have, but I think it needs to be an argument to the decorator, not the handler itself.

like image 200
Steve Losh Avatar answered Oct 12 '22 11:10

Steve Losh