我还是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
答案 0 :(得分:1)
借调itzmeontv所说的继承和一对一是多余的。作为文档引用,您可以通过使用一对一关系或通过创建继承自内置用户模型的模型来扩展用户模型功能。这里的一个问题是你正在做这两件事,这是不必要的,并可能导致混乱。 https://docs.djangoproject.com/en/1.10/topics/auth/customizing/#extending-the-existing-user-model
关于您的特定错误,问题就出现了,因为您正在尝试创建一个具有已存在的用户名的用户对象。您尝试创建的“用户”实际上是具有非唯一用户名的“admin_profile”对象。
我想这个特定问题可以通过让类Profile继承自models.Model而不是User来解决。
from django.db import models
class Profile(models.Model):
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()