2016-11-06 41 views
0

我还是新来的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 
+1

如果添加onetone到'User',则没有必要像继承'个人资料(用户)' – itzMEonTV

+0

这工作,但我不得不删除其移民历史和数据库,使其工作。 – Era

回答

1

借调什么itzmeontv说,关于继承和一到一个是多余的。作为文档参考,您可以通过使用一对一关系或通过继承内置用户模型的模型来扩展用户模型功能。其中一个问题是你正在做这两件事情,这是不必要的,并可能导致混乱。 https://docs.djangoproject.com/en/1.10/topics/auth/customizing/#extending-the-existing-user-model

关于您的具体错误,由于您尝试创建具有已存在用户名的用户对象,因此问题即将到来。您尝试创建的“用户”实际上是具有非唯一用户名的“admin_profile”对象。

我想象一下,这个特殊问题可以通过让类Profile从Model.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() 
+0

你是发现,但我发现,我不能简单地做出这个改变。看起来我以前做过的事情不会正确初始化,并且会导致所有迁移错误。我发现的唯一解决方案是完全删除数据库和迁移,然后进行迁移并在空白的平板上迁移。当我将来会犯这样的错误时,是否有更好的办法,比抹平石板并重新开始。 正如我所说,我是Django的新手,所以重做是一种很好的学习方式,但如果你可以推荐任何更好的方法,那就太棒了。 – Era

+0

如果你设想在开发过程中丢失数据将是一个重复的问题,那么花费一些时间来制作灯具是一个好主意,这对于用初始数据填充数据库非常有用。然后,如果您必须再次删除本地数据库(对于我而言,这在开发过程中会定期发生),您只需运行一个命令并重新获得预期的数据即可。 https://docs.djangoproject.com/zh/1.10/howto/initial-data/ –