Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango user-profile creation code is running twice - why?
    primarykey
    data
    text
    <p>Using Django 1.2, I'm running the <em>django-registration</em> application, and creating a User Profile object after registration created the user - using code that came from some other Questions:</p> <pre><code># models.py def create_player_profile(sender, instance, created, **kwargs): signals.post_save.disconnect(create_player_profile, sender = User) # added. if created: print "creating profile." profile, created = PlayerProfile.objects.get_or_create(user = instance) print "profile %s created = %s" % (str(profile), str(created)) else: print "problems creating profile." signals.post_save.connect(create_player_profile, sender = User) </code></pre> <p>And it works fine :) The only problem is, I get two sets of output, indicating that the code is running <em>twice</em>. I suspect that for some reason, the signal is getting sent twice.</p> <p>My first thought is that the file is getting <em>imported</em> twice, setting up two identical signals. Which makes me wonder if maybe <em>django-registration</em> is doing something automatically? Or something else I don't yet understand about Django. :)</p> <p>So why is the code running twice? Are two signals being sent, and if so, why?</p> <p><strong>Update</strong> Just noticed <a href="https://stackoverflow.com/questions/1478630/django-signal-emitting-once-received-twice-why/1480174#1480174">this Answer</a> explained how to avoid duplicate signals. And it worked, somewhat :) My output went from:</p> <blockquote> <p>creating profile.<br> creating profile.<br> problems creating profile<br> problems creating profile </p> </blockquote> <p>to:</p> <blockquote> <p>creating profile.<br> problems creating profile </p> </blockquote> <p>I don't know if that means my code was originally running <em>four times</em>? And dropped down to only two times? I'm so confused. :)</p> <p>Update 2 - added the <em>signals.disconnect</em> line. I figured deleting the signal would prevent the function from executing twice. Alas, I was wrong - <em>create-player-profile</em> is <em>still</em> running twice... I have no clue why it would, if the signal was immediately deleted.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload