2012-10-12 54 views
0

當我嘗試從shell或通過manage.py或從Django的管理頁面創建超級用戶;它拋出錯誤,如:Django - createsuperuser IntegrityError

IntegrityError at /admin/auth/user/add/ 
null value in column "link_karma" violates not-null constraint 

這裏是我的models.py [用戶部分]:我補充休耕線中settings.py

class User_Profile(models.Model): 

    user = models.OneToOneField(User) 
    link_karma = models.IntegerField() 
    comment_karma = models.IntegerField() 
    avatar = models.CharField(max_length=100) 

    def create_user_profile(sender,instance,created,**kwargs): 
     if created: 
      User_Profile.objects.create(user=instance) 
    post_save.connect(create_user_profile, sender=User) 

    def __unicode__(self): 
     return self.user.username 

AUTH_PROFILE_MODULE = 'accounts.User_Profile' 

之前添加此行它是一樣的。

注意:我試圖改變link_karma字段的類型爲CharField。它是一樣的。

注意:如果需要任何額外的數據;我可以添加它。

+1

修復代碼中的縮進。順便說一句,'用戶'已經有電子郵件字段。 –

+0

修復了縮進。 – alix

+0

我認爲,'create_user_profile'應該是單獨的獨立函數,而不是'User_Profile'方法。 –

回答

1

link_karmacomment_karma嘗試設置blank=True, null=True。您沒有將任何值傳遞給這些字段,因此當您的create語句嘗試創建該對象時,它不知道該給出什麼link_karm因此是錯誤。

正如goliney正確指出的那樣,您不需要用戶已有的User_Profile模型中的電子郵件。

+0

不知道我必須爲IntegerField.thank設置一些東西。 – alix

+0

這沒有解決問題。 – alix

+0

你得到了什麼錯誤?你重新創建了這些字段嗎? – super9