2012-02-15 48 views
1

回溯在'''python manage.py syncdb'''上,並且在密碼輸入兩次後立即發生。數據庫是sqlite3。當我嘗試syncdb時,重複的user_id錯誤來自哪裏?

回溯是:

Traceback (most recent call last): 
    File "manage.py", line 14, in <module> 
    execute_manager(settings) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 438, in execute_manager 
    utility.execute() 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 379, in execute 
    self.fetch_command(subcommand).run_from_argv(self.argv) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 191, in run_from_argv 
    self.execute(*args, **options.__dict__) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 220, in execute 
    output = self.handle(*args, **options) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 351, in handle 
    return self.handle_noargs(**options) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 109, in handle_noargs 
    emit_post_sync_signal(created_models, verbosity, interactive, db) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/core/management/sql.py", line 190, in emit_post_sync_signal 
    interactive=interactive, db=db) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site- packages/django/dispatch/dispatcher.py", line 172, in send 
    response = receiver(signal=self, sender=sender, **named) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/contrib/auth/management/__init__.py", line 70, in create_superuser 
    call_command("createsuperuser", interactive=True) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 166, in call_command 
return klass.execute(*args, **defaults) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 220, in execute 
    output = self.handle(*args, **options) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 134, in handle 
    User.objects.create_superuser(username, email, password) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/contrib/auth/models.py", line 140, in create_superuser 
    u = self.create_user(username, email, password) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/contrib/auth/models.py", line 136, in create_user 
    user.save(using=self._db) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/db/models/base.py", line 460, in save 
    self.save_base(using=using, force_insert=force_insert, force_update=force_update) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/db/models/base.py", line 570, in save_base 
    created=(not record_exists), raw=raw, using=using) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/dispatch/dispatcher.py", line 172, in send 
    response = receiver(signal=self, sender=sender, **named) 
    File "/Users/jonathan/pim/accounts/models.py", line 12, in create_user_profile 
    UserProfile.objects.create(user = instance) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/db/models/manager.py", line 138, in create 
    return self.get_query_set().create(**kwargs) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/db/models/query.py", line 360, in create 
    obj.save(force_insert=True, using=self.db) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/db/models/base.py", line 460, in save 
    self.save_base(using=using, force_insert=force_insert, force_update=force_update) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/db/models/base.py", line 553, in save_base 
    result = manager._insert(values, return_id=update_pk, using=using) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/db/models/manager.py", line 195, in _insert 
    return insert_query(self.model, values, **kwargs) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/db/models/query.py", line 1436, in insert_query 
    return query.get_compiler(using=using).execute_sql(return_id) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 791, in execute_sql 
    cursor = super(SQLInsertCompiler, self).execute_sql(None) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 735, in execute_sql 
    cursor.execute(sql, params) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/db/backends/util.py", line 34, in execute 
    return self.cursor.execute(sql, params) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py", line 234, in execute 
    return Database.Cursor.execute(self, query, params) 
django.db.utils.IntegrityError: column user_id is not unique 

我的models.py低於:

from django.contrib.auth.models import User 
from django.db.models.signals import post_save 
from django.db.models import ForeignKey 
from django.db import models 
from accounts.models import UserProfile 

class CalendarEvent(models.Model): 
    description = models.TextField() 
    user_profile = models.ForeignKey(UserProfile) 
    when = models.TextField() 
    def __init__(self, when, description, user_profile): 
     self.when = when 
     self.description = description 
     self.user_profile = user_profile 
    def accepts(timestamp): 
     if isinstance(timestamp, float): 
      timestamp = time.asctime(time.localtime(timestamp)) 
     if timestamp[:4] == 'days': 
      pass 
     else: 
      if re.match(self.when, timestamp): 
       return True 
      else: 
       return False 

所有user_profile引用刪除下劃線不會改變此行爲。

我在這裏做錯了什麼,我該如何解決它?

- 編輯 -

源的項目是在http://JonathansCorner.com/project/pim.tgz

+1

您可以發佈您創建的帳戶應用程序的model.py,以便我們可以看到您對UserProfile做了什麼?另外,它看起來像你沒有在你的'CalendarEvent .__ init __()' – jdi 2012-02-15 22:39:19

+0

中調用你的超類的'__init __()'。對我來說,看起來類CalendarEvent不負責你得到的錯誤。似乎有一些其他模型的user_id屬性有錯誤,可能是您帳戶應用程序中的UserProfile? – Jingo 2012-02-15 22:39:19

+0

我同意@jdi - 你的'__init__'方法看起來沒有必要。目前還不清楚你想要達到的目標。 Django在聲明模型字段時在幕後做了很多工作,所以您不能像使用常規屬性一樣將它們設置在'__init__'方法中。 – Alasdair 2012-02-15 22:57:32

回答

1

您正在連接User post_save信號兩次,因爲您正在兩個不同路徑上導入帳戶模型模塊。這意味着您正嘗試爲您的新超級用戶創建兩個用戶配置文件,並且第二次失敗。

當Django導入帳戶應用程序時,模型的路徑爲accounts.models。然而,當django導入pim'app'(它實際上不是一個應用程序,它是項目根目錄,稍後更多),accounts.models模塊將作爲pim.accounts.models再次導入。

解決此問題的最快方法是在連接後保存信號時使用unique identifier

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

更好的解決辦法是理清你的進口和項目佈局:

  • 創造的,而不是把它在你的項目根目錄的models.py一個單獨的應用程序包含CalendarEvent模型。
  • 你能說出你的應用項目「PIM」如果你想如果
  • 移動你的models.py,admin.py可能還有一些內部的「PIM」你的網址,以新的應用程序

如果您目前正在開發,那麼最簡單的方法就是刪除用戶配置文件表並讓syncdb重新創建它。或者,您可以刪除外殼中不需要的用戶配置文件。

+0

你在編輯我的答案後添加了這個:-) – jdi 2012-02-16 02:17:16

+0

你打我按下保存編輯按鈕,但是當我開始使用它時,你還沒有提到信號:-) – Alasdair 2012-02-16 02:24:21

+0

save.connect()調用的最佳位置是什麼?我把它放在admin.py中,通過urls.py導入,它抱怨導入用戶(導入是'從django.db.models導入用戶')。我應該把它放在哪裏,還有什麼(如果有的話),我應該這樣做,它符合你的代碼行? – JonathanHayward 2012-02-16 16:11:36

1

注:我以前的答案在這個地方正確答案已移除...

的問題是,你的post_save信號連接多次每次accounts.models是什麼進口的時間。你真正想要做的是重新定位到management.py文件的信號連接帳戶應用內

賬戶/ models.py

from django.contrib.auth.models import User 
from django.db import models 

class UserProfile(models.Model): 
    name = models.TextField() 
    scratchpad = models.TextField() 
    user = models.OneToOneField(User) 
    timezone_offset = models.IntegerField(default = 0) 


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

帳戶/管理。PY

from django.db.models.signals import post_save 
from accounts.models import create_user_profile 
from django.contrib.auth.models import User 

post_save.connect(create_user_profile, sender = User) 

Django會加載management.py模塊一次,所以無論你accounts.models多少次進口現在只連接一個信號。

+0

我剛剛修復了我的答案。前一個是錯的。是的,我同意,雖然一切應該真的是一個應用程序,並沒有在根源模型。但真正的原因是他的信號連接了很多次。 – jdi 2012-02-16 01:44:24

+0

這兩個答案讓我很遺憾,我只能將一個答案標記爲正確。謝謝,並分享到應用程序。 – JonathanHayward 2012-02-16 15:18:50

+0

你只能標記一個答案是正確的,但你仍然可以upvote他的答案;) – Alasdair 2012-02-16 16:42:29