Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7081825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:58:11+00:00 2026-05-28T06:58:11+00:00

In views.py I have the following view which gets called when a new user

  • 0

In views.py I have the following view which gets called when a new user account is registered. All it does is get the username, email, and password from the request, then try to create a user with those credentials. In the code below, “A” gets printed, but “B” does not, because it crashes:

views.py

def register(request):
    if request.method == 'POST':
        query_dict = request.POST
        username = query_dict['username']
        email = query_dict['user_email']
        password = query_dict['password']
        role = query_dict['role']
        print "A"
        user = User.objects.create_user(username, email, password)
        # the handler is called here and creates the user profile
        print "B"
        user = authenticate(username=username, password=password)
        user_profile = user.get_profile()
        user_profile.role = role
        user_profile.save()
        if user is not None and user.is_active:
            login(request, user)
            return HttpResponseRedirect("/")

In myapp/models.py I have the following code for the handler.

'models.py`

post_save.connect(create_user_profile, sender=User)

def create_user_profile(sender, instance, created, **kwargs):
    print "created="+str(created)
    if created: # as opposed to being saved after being created earlier, I assume
        print "attempting to create a user profile for "+instance.username
        up = UserProfile.objects.create(user=instance)
        print "created a new UserProfile: <"+str(up)+">"

When I run my app, the print output is:

A
created=True
attempting to create a user profile for john
created a new UserProfile: <john - >
created=True
attempting to create a user profile for john
~~~~CRASHES~~~~

I have noticed throughout my development over the last couple of days that the create_user_profile method gets called twice when I run User.objects.create_user(username, email, password) I copied the handler code directly out of the docs, so I’m pretty sure it correct (https://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users). I don’t know exactly where in the django code the create_user_profile method gets called from, but I am assuming that it sets the created flag to true only if the save method that invoked the handler was due to an object being created. Over the last couple of days this code has been working correctly, but all of a sudden today the second time the create_user_profile method gets called by the handler, the created flag is set to true, just like it was the first time. This is causing my app to crash with an IntegrityError, complaining that it can’t create two objects with the same primary key.

So there are two things I don’t understand. First: why does post_save happen twice when I only called User.objects.create_user(username, email, password) once; second: why did the created flag all of a sudden stop working like expected?

How can I debug this?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:58:11+00:00Added an answer on May 28, 2026 at 6:58 am

    It is possible that your models.py file is being imported, and run, twice. In this case, the same signal handler would be attached twice, and would fire twice when a model instance is saved.

    This can easily happen if you import it once as

    import myapp.models
    

    and again as

    import myproject.myapp.models
    

    Printing something to the console at the top of the file might give you an idea whether that is what is happening or not.

    Regardless, you can prevent a signal handler from being registered multiple times by giving it a unique dispatch_uid. Django will check on Signal.connect to see if a handler has already been registered for the same sender with the same UID, and will not re-register it if it has.

    Change your signal connecting code to something like

    post_save.connect(create_user_profile, sender=User, dispatch_uid='create_user_profile')
    

    and see if that helps

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view with which I have a button calling the following method.
Lets assume I have the following 3 entities: Customer,Order,Product which interact in the View
in a partial view I have the following: <%Html.RenderAction(MVC.User.GetComments(Model.UserGroupName)); %> can I render a
I have a view in which I paint a dotted line when the user
I have following code which add BackButton on my view's navigation item's tabbar. It
I have a button in my activity, pressing upon which, the following method gets
I have following view <asp:Content ID=Content2 ContentPlaceHolderID=MainContent runat=server> <h2>Tables <%=ViewData[RetriverName] %></h2> <%using (Html.BeginForm(ResfreshSelectedTables, Home))
In a branch spec, I have the following view: //depot/dev/t/a/g/... //depot/dev/t/r/g/... -//depot/dev/t/a/g/p/o*/... //depot/dev/t/r/g/p/... Perforce
I have an ASP MVC view where have the following statement #if DEBUG //section
I have the following in my view: <%using (Html.BeginForm()) {%> <% foreach (var item

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.