0
votes

I am seeing an error with Django user profile creation that has been asked before a few times, but the solutions to the other questions and the Django docs recommended fix is not working.

I have a UserProfile model that I am trying to create when creating a new user object from the Django admin pages.(forgive me on syntax here, it has been a while since I used stack overflow)

class UserProfile(models.Model):  
    user = models.OneToOneField(User)
    organization = models.IntegerField(null=True)
    phone = models.CharField(max_length=20, null=True, blank=True)
    phone_ext = models.CharField(max_length=8, null=True, blank=True)
    team_no = models.CharField(max_length=30, null=True, blank=True)    

post_save.connect(create_user_profile, sender=User, dispatch_uid="a.unique.string.that.does.not.help.me")

User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])

I have added the user profile to the UserAdmin class and unregistered/registered the admin site entries.

class UserProfileInline(admin.StackedInline):
    model = UserProfile
    can_delete = False
    verbose_name_plural = 'profile'



class UserAdmin(UserAdmin):
    inlines = (UserProfileInline, )
    actions = [set_organization]
    exclude = ('is_staff',)


try:
    admin.site.unregister(User)
    admin.site.unregister(RegistrationProfile)
    admin.site.unregister(Group)
## Re-register UserAdminadmin.site.unregister(User)
finally:
    admin.site.register(User, UserAdmin)
    admin.site.register(RegistrationProfile, RegistrationAdmin)
    admin.site.register(Group, GroupAdmin)

So each time that I try to go create a new user I always get a duplicate key error for the user_profile_user_id field. Looking at the logs there is two insert statements being ran on the user save. The first is missing all data input into the user profile fields in the admin interface. The second has all the data but fails since there was already the initial insert with just he user_id.

I have searched the project over a few times looking for a duplicate import of my models.py for the UserProfile class and have found nothing.

Does anyone have any ideas here?

Thank you.

--update --

Django version is 1.3

imports in the profile/models.py
from django.contrib.auth.models import User
from django.db.models.signals import post_save, pre_save
import logging

imports from the admin.py from django.contrib import admin from django.contrib.auth.models import Group from test.django.registration.models import RegistrationProfile from test.django.registration.admin import RegistrationAdmin from django.contrib.auth.admin import UserAdmin, GroupAdmin from django.contrib.auth.models import User from test.django.extranet.profile.models import UserProfile from test.django.common.models.ajrsonline.ajrsonline_users import AjrsonlineUsers from test.django.common.middleware.ajrs_ldap import AJRSOnlineLdapBackend

1
what version of Django and could you post your imports? - Glyn Jackson
Glyn, thanks for the response, I had already checked question 2345400 and changing the entry for installed apps did not change anything. - grantk
Why do you have BOTH a signal and a lambda which creates the profile? The problem is obviously related to that get_or_create call. I don't really understand the design choice. I'm betting that, somewhere deep in Django, you're hitting a condition where the property is being accessed (hence creating the record) and THEN the signal is firing, where you're also trying to create the record. Django isn't catching the record was already created, so it tries to create it again. That weird property thing is weird. I don't get it. - Jack Shedd
Jack, The lambda was there to create the profile automatically, this was the problem. Each time the method was called an empty profile was being created, then the second get_or_create was running with the form data. Create an answer if you like and I will choose that as I would not like to have the question unanswered. - grantk

1 Answers

0
votes

Use if statement so that the first one will be restricted. Like this for example:

 if instance.field_name:
     //save data here

     //In here you can save the value that has complete data