2012-04-13 5 views
2

create_user_profile関数を使用してユーザーを作成して接続しました。ユーザーを作成するときに問題が発生しました。接続された関数が2回呼び出され、UserPRofileが2回作成されようとしています。djangoがユーザー作成時に信号を送信

列のuser_idはここ

Request Method: POST 
Request URL: http://127.0.0.1:8000/register/ 
Django Version: 1.3 
Exception Type: IntegrityError 
Exception Value:  
column user_id is not unique 
Exception Location: c:\Python27\lib\site-packages\django\db\backends\sqlite3\base.py in execute, line 234 
Python Executable: c:\Python27\python.exe 
Python Version: 2.7.2 

一意でないデバッグログです:

[13/Apr/2012 10:20:57] "GET /register/ HTTP/1.1" 200 176691 
DEBUG:django.db.backends:(0.002) SELECT "auth_user"."id", "auth_user"."username", 
"auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_us 
er"."password", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."is_ 
superuser", "auth_user"."last_login", "auth_user"."date_joined" FROM "auth_user" 
WHERE "auth_user"."last_name" = favicon ; args=(u'favicon',) 
DEBUG:django.db.backends:(0.001) SELECT "auth_user"."id", "auth_user"."username", 
"auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_us 
er"."password", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."is_ 
superuser", "auth_user"."last_login", "auth_user"."date_joined" FROM "auth_user" 
WHERE "auth_user"."last_name" = testuser2 ; args=(u'testuser2',) 
DEBUG:django.db.backends:(0.000) SELECT "auth_user"."id", "auth_user"."username", 
"auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_us 
er"."password", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."is_ 
superuser", "auth_user"."last_login", "auth_user"."date_joined" FROM "auth_user" 
WHERE "auth_user"."username" LIKE [email protected] ESCAPE '\' ; args=(u'testus 
[email protected]',) 
DEBUG:django.db.backends:(0.001) INSERT INTO "auth_user" ("username", "first_name 
", "last_name", "email", "password", "is_staff", "is_active", "is_superuser", "la 
st_login", "date_joined") VALUES ([email protected], , , [email protected], s 
ha1$9bc1b$c9d377a1bf228ef74eb6931a9ed02d5c455e1576, False, True, False, 2012-04-1 
3 10:21:13.949000, 2012-04-13 10:21:13.949000); args=(u'[email protected]', '', 
'', u'[email protected]', 'sha1$9bc1b$c9d377a1bf228ef74eb6931a9ed02d5c455e1576 
', False, True, False, u'2012-04-13 10:21:13.949000', u'2012-04-13 10:21:13.94900 
0') 


==================================== 
= 


DEBUG:django.db.backends:(0.002) INSERT INTO "page_userprofile" ("user_id", "thum 
b", "name", "title", "description", "phone", "birthday", "address", "lat", "lng", 
"gender", "creation_date", "updation_date") VALUES (13, , , , , , None, , , , , 
2012-04-13 10:21:14.131000, 2012-04-13 10:21:14.131000); args=(13, '', '', '', '' 
, '', None, '', '', '', '', u'2012-04-13 10:21:14.131000', u'2012-04-13 10:21:14. 
131000') 
DEBUG:django.db.backends:(0.002) INSERT INTO "page_pagesettings" ("user_id", "bac 
kground_url", "background_repeat", "showAvatar", "showTags", "color_background", 
"color_panel_background", "color_panel_background_opacity", "color_name", "color_ 
title", "color_description", "color_links", "font_name", "font_title", "font_desc 
ription", "font_tags_and_links", "font_size_name", "font_size_title", "font_size_ 
description", "font_size_tags_and_links", "can_contact_via_email", "panel_positio 
n") VALUES (13, , 0, False, True, , , , , , , , , , , , , , , , True,); args=(13 
, '', u'0', False, True, '', '', '', '', '', '', '', '', '', '', '', '', '', '', 
'', True, '') 


==================================== 
= 


DEBUG:django.db.backends:(0.002) INSERT INTO "page_userprofile" ("user_id", "thum 
b", "name", "title", "description", "phone", "birthday", "address", "lat", "lng", 
"gender", "creation_date", "updation_date") VALUES (13, , , , , , None, , , , , 
2012-04-13 10:21:14.442000, 2012-04-13 10:21:14.442000); args=(13, '', '', '', '' 
, '', None, '', '', '', '', u'2012-04-13 10:21:14.442000', u'2012-04-13 10:21:14. 
442000') 
DEBUG:django.db.backends:(0.000) SELECT "page_userprofile"."id", "page_userprofil 
e"."user_id", "page_userprofile"."thumb", "page_userprofile"."name", "page_userpr 
ofile"."title", "page_userprofile"."description", "page_userprofile"."phone", "pa 
ge_userprofile"."birthday", "page_userprofile"."address", "page_userprofile"."lat 
", "page_userprofile"."lng", "page_userprofile"."gender", "page_userprofile"."cre 
ation_date", "page_userprofile"."updation_date" FROM "page_userprofile" LIMIT 21; 
args=() 
DEBUG:django.db.backends:(0.000) SELECT "auth_user"."id", "auth_user"."username", 
"auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_us 
er"."password", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."is_ 
superuser", "auth_user"."last_login", "auth_user"."date_joined" FROM "auth_user" 
WHERE "auth_user"."id" = 1 ; args=(1,) 
DEBUG:django.db.backends:(0.000) SELECT "auth_user"."id", "auth_user"."username", 
"auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_us 
er"."password", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."is_ 
superuser", "auth_user"."last_login", "auth_user"."date_joined" FROM "auth_user" 
WHERE "auth_user"."id" = 2 ; args=(2,) 
DEBUG:django.db.backends:(0.000) SELECT "auth_user"."id", "auth_user"."username", 
"auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_us 
er"."password", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."is_ 
superuser", "auth_user"."last_login", "auth_user"."date_joined" FROM "auth_user" 
WHERE "auth_user"."id" = 4 ; args=(4,) 
DEBUG:django.db.backends:(0.000) SELECT "auth_user"."id", "auth_user"."username", 
"auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_us 
er"."password", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."is_ 
superuser", "auth_user"."last_login", "auth_user"."date_joined" FROM "auth_user" 
WHERE "auth_user"."id" = 5 ; args=(5,) 
DEBUG:django.db.backends:(0.000) SELECT "auth_user"."id", "auth_user"."username", 
"auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_us 
er"."password", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."is_ 
superuser", "auth_user"."last_login", "auth_user"."date_joined" FROM "auth_user" 
WHERE "auth_user"."id" = 7 ; args=(7,) 
DEBUG:django.db.backends:(0.000) SELECT "auth_user"."id", "auth_user"."username", 
"auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_us 
er"."password", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."is_ 
superuser", "auth_user"."last_login", "auth_user"."date_joined" FROM "auth_user" 
WHERE "auth_user"."id" = 8 ; args=(8,) 
DEBUG:django.db.backends:(0.001) SELECT "auth_user"."id", "auth_user"."username", 
"auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_us 
er"."password", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."is_ 
superuser", "auth_user"."last_login", "auth_user"."date_joined" FROM "auth_user" 
WHERE "auth_user"."id" = 9 ; args=(9,) 
DEBUG:django.db.backends:(0.001) SELECT "auth_user"."id", "auth_user"."username", 
"auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_us 
er"."password", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."is_ 
superuser", "auth_user"."last_login", "auth_user"."date_joined" FROM "auth_user" 
WHERE "auth_user"."id" = 10 ; args=(10,) 
DEBUG:django.db.backends:(0.000) SELECT "auth_user"."id", "auth_user"."username", 
"auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_us 
er"."password", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."is_ 
superuser", "auth_user"."last_login", "auth_user"."date_joined" FROM "auth_user" 
WHERE "auth_user"."id" = 11 ; args=(11,) 
DEBUG:django.db.backends:(0.000) SELECT "auth_user"."id", "auth_user"."username", 
"auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_us 
er"."password", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."is_ 
superuser", "auth_user"."last_login", "auth_user"."date_joined" FROM "auth_user" 
WHERE "auth_user"."id" = 12 ; args=(12,) 
DEBUG:django.db.backends:(0.000) SELECT "auth_user"."id", "auth_user"."username", 
"auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_us 
er"."password", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."is_ 
superuser", "auth_user"."last_login", "auth_user"."date_joined" FROM "auth_user" 
WHERE "auth_user"."id" = 13 ; args=(13,) 
[13/Apr/2012 10:21:15] "POST /register/ HTTP/1.1" 500 446959 
DEBUG:django.db.backends:(0.001) SELECT "auth_user"."id", "auth_user"."username", 
"auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_us 
er"."password", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."is_ 
superuser", "auth_user"."last_login", "auth_user"."date_joined" FROM "auth_user" 
WHERE "auth_user"."last_name" = favicon ; args=(u'favicon',) 

接続:ユーザーを作成

def create_user_profile(sender, instance, created, **kwargs): 
    if created: 
     print('') 
     print('') 
     print('=========================================') 
     print('') 
     print('') 
     UserProfile.objects.create(user=instance) 
     PageSettings.objects.create(user=instance) 

post_save.connect(create_user_profile, sender=User) 

newUser = User.objects.create_user(username, username, password) 
newUser.last_name = slug 

if(registerAs == 'company'): 
    newUser.first_name = 'company' 
else: 
    newUser.first_name = 'user' 

#newUser.save() 
+0

を適切にあなたのコードをフォーマットしてください。 – cfedermann

答えて

9

あなたの信号一意の識別子を与えることdispatch_uid引数を使用します。

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

詳細については、django docsを参照してください。

+1

これはシグナルが複数回*接続されないようにするためです。アクション*が複数回発生する場合は、*複数回*発火します。 –

+0

しかし、プロファイルは['created'](https://docs.djangoproject.com/en/dev/ref/signals/#django.db.models.signals.post_save)が' True'の場合にのみ作成されます。後続のセーブでは 'created'が' False'になります。 – Alasdair

3

あなたはあなたの方法に追加の健全性チェックを追加することで問題を回避することができます

def create_user_profile(sender, instance, created, **kwargs): 
    if created and not UserProfile.objects.exists(user=instance): 
     UserProfile.objects.create(user=instance) 
     PageSettings.objects.create(user=instance) 
+2

この健全性チェックはうまくいくと思いますが、 'dispatch_uid'を指定することで根本的な問題(' create_user_profile'受信者関数が2回登録されている)に対処する方が良いと思います。 – Alasdair

+2

もちろん。それは常に根の悪を修正する方が良いです:) – cfedermann

関連する問題