ignabe
ignabe

Reputation: 2320

Django Signals: How to initialize bindings?

I'm importing the signals file on the __init__.py file of the App. The issue happens when I run manage.py runserver i found that the __init__.py is imported two times, and my callbacks run two times.

But, when I run manage.py shell, the __init__.py it's worked as expected (one time)

(Django 1.8.3)

app/__init__.py:

print 'Hello!'
import signals

app/models.py:

from django.db import models

class RemoteTask(models.Model):
    title = models.CharField(max_length=50)

class RemoteTaskStatus(models.Model):
    remote_task = models.IntegerField()
    status = models.IntegerField()

app/signals.py:

print '------> app/signals.py'

from django.db.models.signals import post_save
from django.dispatch import receiver
from app.models import RemoteTask, RemoteTaskStatus

@receiver(post_save, sender=RemoteTask)
def status_new(sender, **kwargs):
    print '---------> Running callback'
    if kwargs.get('created', 'False') is True:
        instance = kwargs['instance']
        RemoteTaskStatus.objects.create(
            remote_task=instance,
            status=0
        )

Example output:

$ ./manage.py runserver
Hello!
------> app/signals.py
Hello!
------> app/signals.py
Performing system checks...

System check identified no issues (0 silenced).
August 12, 2015 - 19:50:38
Django version 1.8.3, using settings 'signal_loca.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Solution from Angel Velasquez: app/__init__.py:

print 'Hello!'
import app.signals

Upvotes: 2

Views: 2712

Answers (2)

Angel Velásquez
Angel Velásquez

Reputation: 778

Hmm I've read that you were running tests? by any chance were py.test ? try deleting .pyc files or the __pycache__ folder.

Upvotes: 1

chucksmash
chucksmash

Reputation: 6017

Import your signals.py inside of an AppConfig.ready method to prevent the callback from being registered twice. Here's the recommended way to do this:

my_awesome_project/
|-- app/
|   |-- __init__.py   <-- set default_app_config here
|   |-- apps.py       <-- define your AppConfig subclass here
|   |-- models.py
|   |-- signals.py    <-- define your signal handling functions here
|-- other_app/
|-- my_awesome_project/
    |-- __init__.py
    |-- settings.py
    |-- wsgi.py

app/__init__.py

default_app_config = 'app.apps.MyAppConfig'

app/apps.py

from django.apps import AppConfig

class MyAppConfig(AppConfig):
    name="app"

    def ready(self):
        from . import signals

apps/models.py

from django.db import models

class RemoteTask(models.Model):
    title = models.CharField(max_length=50)

class RemoteTaskStatus(models.Model):
    remote_task = models.IntegerField()
    status = models.IntegerField()

app/signals.py

from django.db.models.signals import post_save
from django.dispatch import receiver
from app.models import RemoteTask, RemoteTaskStatus

@receiver(post_save, sender=RemoteTask, dispatch_uid="status_new_signal_on_RemoteTask_post_save")
def status_new(sender, **kwargs):
    print '---------> Running callback'
    if kwargs.get('created', 'False') is True:
        instance = kwargs['instance']
        RemoteTaskStatus.objects.create(
            remote_task=instance,
            status=0
        )

Edit:

Added dispatch_uid to receiver decorator.

Upvotes: 9

Related Questions