suhail areekkan suhail areekkan - 2 months ago 36
Python Question

Django signals not receiving/working

i am using django default receiver to handle signal. but its not working.
i have modified User model in APP1 whenver new User object create a receiver in APP2 signal.py is listen to it, but its not working.

app1/model.py



class User(BaseModel, AbstractBaseUser, PermissionsMixin):
email = models.EmailField(
verbose_name='email address',
max_length=255,
unique=True,
)
username = models.CharField(max_length=40, unique=True)
first_name = models.CharField(max_length=30, blank=True, null=True)
last_name = models.CharField(max_length=30, blank=True, null=True)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
is_staff = models.BooleanField(default=False)
is_email_verified = models.BooleanField(default=False)
is_paid = models.IntegerField(default=0)
access_token = models.CharField(max_length=128, blank=True)

objects = UserManager()

USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username']

class Meta:
db_table = 'users'

def __str__(self):
return self.email


app2/signals.py



from django.conf import settings
from django.db.models.signals import post_save
from django.dispatch import receiver
from accounts.models import User

@receiver(post_save, sender=User)#settings.AUTH_USER_MODEL)
def create_auth_token(sender, instance=None, created=False, **kwargs):
print ("token generated")
if created:
print("data at signal ****", instance.email, instance)

Answer Source

finaly i got the answer . its because i'm not importing signal.py on startup.

working code

apps.py

from __future__ import unicode_literals

from django.apps import AppConfig


    class StreamsConfig(AppConfig):
        name = 'streams'    

        def ready(self):
            print("at ready")
            import streams.signals

init.py

default_app_config = 'streams.apps.StreamsConfig'

after this change i'm receiving signals