Search code examples
djangodjango-signals

django how do i send a post_save signal when updating a user?


having read the docs,

https://docs.djangoproject.com/en/dev/topics/signals/

i have created this in my signals.py file:

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

from models import Story

@receiver(post_save, sender=User)
def create_initial_story(sender,instance, signal, created, **kwargs):
    print "helloooo!"
    if created:
        Story(user = instance, title = 'Random Stories', description="Random stories", is_closed = False, is_random = True).save()

which, from what i read, was all i thought i needed to do to send a message. Well, that and create a new user (i use the django-registration framework) However, nothing is being sent (well, the receiver method i have does nothing). I also removed the "sender=User" parameter to the @receiver annotation - leaving

@receiver(post_save)

but that did not help matters. Nothing is output to the console, no new data is saved... do i need to send the signal from the User when the User is saved?? If so, how would i do that? I am using django-registration, so i have a UserProfile defined... what i mean is, where (in what file/method) would i tell the User to send the signal?


Solution

  • You should import your signals.py somewhere to run it. For example in models.py.