gpt4 book ai didi

填写个人资料字段时,Django 用户创建在管理中失败

转载 作者:行者123 更新时间:2023-12-02 13:16:55 25 4
gpt4 key购买 nike

我正在使用 Django 1.4.1 和 postgresql 9.1。

我需要向身份验证应用程序指定的用户添加配置文件,并允许管理应用程序创建和编辑此配置文件。因此,我一直在关注文档部分 Storing additional information about users :

模型.py
class UserProfile(models.Model):
user = models.OneToOneField(User)

bio = models.TextField(null = True, blank = True)
contact = models.TextField(null = True, blank = True)

def create_user_profile(sender, instance, created, **kwargs):
if created:
UserProfile.objects.create(user=instance)

post_save.connect(create_user_profile, sender=User)

设置.py
...
AUTH_PROFILE_MODULE = 'userprofile.UserProfile'
...

我还在 INSTALLED_APPS 中激活了 django.contrib.authdjango.contrib.admin 应用。

admin.py
class UserProfileInline(admin.StackedInline):
model = UserProfile
can_delete = False
verbose_name_plural = 'profile'

class UserAdmin(UserAdmin):
inlines = (UserProfileInline, )

# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, UserAdmin)

问题

现在,当我运行管理应用程序并要求添加(创建)新用户时,系统会要求我通过两步过程创建用户:首先,一个页面要求只有用户名、密码(两次)和我的两个 UserProfile 字段。

如果我仅输入用户名和密码(两次)并单击“保存”,则会显示该过程的第二页,其中允许填写所有其他用户字段以及我的用户配置文件字段。出现一条消息说“用户“xxxxx”添加成功。您可以在下面再次编辑它。”,幸运的是我可以编辑两个模型的字段,它可以工作。

但是,如果我尝试在第一页的一个或两个 UserProfile 字段中输入任何内容,则提交会失败并显示以下消息:

IntegrityError at /admin/auth/user/add/

duplicate key value violates unique constraint "userprofile_userprofile_user_id_key"
DETAIL: Key (user_id)=(7) already exists.

每次我尝试时,“7”都会递增。

如何避免这种行为,或者如何防止配置文件字段在第一页中可编辑,但让它们在第二页中编辑?

完整回溯:

Environment:Request Method: POSTRequest URL: http://127.0.0.1:8000/admin/auth/user/add/Django Version: 1.4.1Python Version: 2.7.3Installed Applications:('django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.admin', 'django.contrib.admindocs', 'userprofile')Installed Middleware:('django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware')Traceback:File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response  111.                         response = callback(request, *callback_args, **callback_kwargs)File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py" in wrapper  366.                 return self.admin_site.admin_view(view)(*args, **kwargs)File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in _wrapped_view  91.                     response = view_func(request, *args, **kwargs)File "/usr/local/lib/python2.7/dist-packages/django/views/decorators/cache.py" in _wrapped_view_func  89.         response = view_func(request, *args, **kwargs)File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/sites.py" in inner  196.             return view(request, *args, **kwargs)File "/usr/local/lib/python2.7/dist-packages/django/views/decorators/debug.py" in sensitive_post_parameters_wrapper  69.             return view(request, *args, **kwargs)File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in _wrapper  25.             return bound_func(*args, **kwargs)File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in _wrapped_view  91.                     response = view_func(request, *args, **kwargs)File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in bound_func  21.                 return func(self, *args2, **kwargs2)File "/usr/local/lib/python2.7/dist-packages/django/db/transaction.py" in inner  209.                 return func(*args, **kwargs)File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/admin.py" in add_view  114.                                                extra_context)File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in _wrapper  25.             return bound_func(*args, **kwargs)File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in _wrapped_view  91.                     response = view_func(request, *args, **kwargs)File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in bound_func  21.                 return func(self, *args2, **kwargs2)File "/usr/local/lib/python2.7/dist-packages/django/db/transaction.py" in inner  209.                 return func(*args, **kwargs)File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py" in add_view  956.                 self.save_related(request, form, formsets, False)File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py" in save_related  733.             self.save_formset(request, form, formset, change=change)File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py" in save_formset  721.         formset.save()File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py" in save  497.         return self.save_existing_objects(commit) + self.save_new_objects(commit)File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py" in save_new_objects  628.             self.new_objects.append(self.save_new(form, commit=commit))File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py" in save_new  731.             obj.save()File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in save  463.         self.save_base(using=using, force_insert=force_insert, force_update=force_update)File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in save_base  551.                 result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py" in _insert  203.         return insert_query(self.model, objs, fields, **kwargs)File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py" in insert_query  1576.     return query.get_compiler(using=using).execute_sql(return_id)File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py" in execute_sql  910.             cursor.execute(sql, params)File "/usr/local/lib/python2.7/dist-packages/django/db/backends/util.py" in execute  40.             return self.cursor.execute(sql, params)File "/usr/local/lib/python2.7/dist-packages/django/db/backends/postgresql_psycopg2/base.py" in execute  52.             return self.cursor.execute(query, args)Exception Type: IntegrityError at /admin/auth/user/add/Exception Value: duplicate key value violates unique constraint "userprofile_userprofile_user_id_key"DETAIL:  Key (user_id)=(7) already exists.`

最佳答案

正如CadentOrange在评论中提到的,此问题的解决方案在this answer中有描述。 .

问题在于使用内联管理表单。发生的情况如下:

  1. 它保存主模型(用户)
  2. 由于 (1),Userpost_save 信号处理程序被触发,从而创建一个新的 UserProfile 对象
  3. 每个内联模型都会保存(包括UserProfile另一个副本,从而导致欺骗)。

关于填写个人资料字段时,Django 用户创建在管理中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12784830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com