回溯位于'''python manage.py syncdb'''上,并在输入密码两次后立即发生。数据库是sqlite3。
追溯是:
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引用中删除下划线不会改变此行为。
我在这里做错了什么,如何解决?
- 编辑 -
答案 0 :(得分: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")
更好的解决方法是整理您的导入和项目布局:
如果您当前正在开发,那么最简单的方法是删除用户配置文件表并让syncdb重新创建它。或者,您可以删除shell中不需要的用户配置文件。
答案 1 :(得分:1)
注意:我之前的答案已被删除,取代了这个正确答案......
问题是,每当accounts.models
导入任何内容时,您的post_save信号都会连接多次。您真正想要做的是将信号连接重新定位到帐户应用内的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)
帐户/ management.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导入了多少次。