我还是新来的Django,我一直在阅读所有以前的用户配置文件,并尝试采用找到的解决方案。这样做,我一直无法通过shell实际上将配置文件保存到数据库。相反,我收到了'IntegrityError'。用户配置文件不保存配置文件和抛出异常
我的个人资料型号:
class Profile(User):
user = models.OneToOneField(User, on_delete=models.CASCADE)
chunks = models.ManyToManyField(Chunk)
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()
我已经改变了我的设置,通过在设置下列授权该形象模块:努力为用户创建配置文件时发生
AUTH_PROFILE_MODULE = 'pomodoro.Profile'
我的问题。 在shell我都试过,但得到的AUTH错误:
from django.contrib.auth.models import User
from pomodoro.models import Profile
admin_profile = Profile()
admin = User.objects.get(pk=1)
admin_profile.user = admin
admin_profile.save()
Traceback (most recent call last):
File "C:\Python35\lib\site-packages\django\db\backends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "C:\Python35\lib\site-packages\django\db\backends\sqlite3\base.py", line 337, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.IntegrityError: UNIQUE constraint failed: auth_user.username
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Python35\lib\site-packages\django\contrib\auth\base_user.py", line 80, in save
super(AbstractBaseUser, self).save(*args, **kwargs)
File "C:\Python35\lib\site-packages\django\db\models\base.py", line 796, in save
force_update=force_update, update_fields=update_fields)
File "C:\Python35\lib\site-packages\django\db\models\base.py", line 823, in save_base
self._save_parents(cls, using, update_fields)
File "C:\Python35\lib\site-packages\django\db\models\base.py", line 848, in _save_parents
self._save_table(cls=parent, using=using, update_fields=update_fields)
File "C:\Python35\lib\site-packages\django\db\models\base.py", line 889, in _save_table
forced_update)
File "C:\Python35\lib\site-packages\django\db\models\base.py", line 939, in _do_update
return filtered._update(values) > 0
File "C:\Python35\lib\site-packages\django\db\models\query.py", line 654, in _update
return query.get_compiler(self.db).execute_sql(CURSOR)
File "C:\Python35\lib\site-packages\django\db\models\sql\compiler.py", line 1148, in execute_sql
cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
File "C:\Python35\lib\site-packages\django\db\models\sql\compiler.py", line 835, in execute_sql
cursor.execute(sql, params)
File "C:\Python35\lib\site-packages\django\db\backends\utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "C:\Python35\lib\site-packages\django\db\backends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "C:\Python35\lib\site-packages\django\db\utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "C:\Python35\lib\site-packages\django\utils\six.py", line 685, in reraise
raise value.with_traceback(tb)
File "C:\Python35\lib\site-packages\django\db\backends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "C:\Python35\lib\site-packages\django\db\backends\sqlite3\base.py", line 337, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: UNIQUE constraint failed: auth_user.username
如果添加onetone到'User',则没有必要像继承'个人资料(用户)' – itzMEonTV
这工作,但我不得不删除其移民历史和数据库,使其工作。 – Era