2012-02-15 46 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