1

I think it's a bug on Django administration, when you add a user on the administration site and you have a automatic creation profil on your model, the site crash, due to a problem with the instance witch create the profil :

def create_user_profile(sender, instance, created, **kwargs) :

    if created :
        data = Profile.objects.get_or_create(user = instance)
        profile = data[0]

        # Initialisation du profil
        profile.user = instance
        profile.credits = 0
        profile.score = 0

        profile.save()

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

on the admin site, the instance is not the user, the instance is the administrator user.

the solution is to delete this code when you create a user on the admin site, what about another solution ? this is annoying...

Alexander Larikov
  • 2,328
  • 15
  • 15
cedric
  • 302
  • 1
  • 12

1 Answers1

1

Try commenting out the line profile.user = instance. The profile object that was created or retrieved already has the user set by get_or_create.

dgel
  • 16,352
  • 8
  • 58
  • 75
  • Sorry, i have complete my code. The problem is, get_or_create take my profil (the admin profil because i'm log in the admin site), but, it should take the user profil i have created.. – cedric Jul 31 '12 at 08:50
  • (1062, "Duplicate entry '9' for key 'user_id'") – cedric Jul 31 '12 at 08:56
  • `get_or_create` shouldn't cause that- are you sure this code is causing that error? – dgel Jul 31 '12 at 09:01
  • no, i mean i'm sure this line is the problem, of course if i delete the problem, there is no problem lol :) but it doesnt resolve it. i want to keep this code for auto creating the profil of the user but, it doesnt work when i create a user in the admin site, i think the problem is the same of this thread : http://stackoverflow.com/questions/2813189/django-userprofile-with-unique-foreign-key-in-django-admin The admin site save the profil before the model does... – cedric Jul 31 '12 at 09:19
  • i have resolve my problem by override the save method of the profil. like they said in [this thread](http://stackoverflow.com/questions/2813189/django-userprofile-with-unique-foreign-key-in-django-admin) – cedric Jul 31 '12 at 09:27