我看到Django用户配置文件创建过程中出现错误,但在其他问题的解决方案和修补程序无法正常工作。Django用户配置文件创建发生两次
我有我试图从Django的管理页面中创建一个新的用户对象时创建一个用户配置模式。(原谅我的语法在这里,它已经有一段时间,因为我用堆栈溢出)
class UserProfile(models.Model):
user = models.OneToOneField(User)
organization = models.IntegerField(null=True)
phone = models.CharField(max_length=20, null=True, blank=True)
phone_ext = models.CharField(max_length=8, null=True, blank=True)
team_no = models.CharField(max_length=30, null=True, blank=True)
post_save.connect(create_user_profile, sender=User, dispatch_uid="a.unique.string.that.does.not.help.me")
User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])
我已将用户配置文件添加到UserAdmin类中,并取消了注册/注册管理网站条目。
class UserProfileInline(admin.StackedInline):
model = UserProfile
can_delete = False
verbose_name_plural = 'profile'
class UserAdmin(UserAdmin):
inlines = (UserProfileInline,)
actions = [set_organization]
exclude = ('is_staff',)
try:
admin.site.unregister(User)
admin.site.unregister(RegistrationProfile)
admin.site.unregister(Group)
## Re-register UserAdminadmin.site.unregister(User)
finally:
admin.site.register(User, UserAdmin)
admin.site.register(RegistrationProfile, RegistrationAdmin)
admin.site.register(Group, GroupAdmin)
所以每次我尝试去创建一个新用户,我总是得到一个重复键错误的user_profile_user_id场时间。查看日志,在用户保存上运行两条插入语句。首先是缺少输入到管理界面中用户配置文件字段的所有数据。第二个包含所有数据但失败,因为已经有了user_id的初始插入。
我已经搜索了几次项目,寻找UserProfile类的models.py的重复导入,但没有找到任何东西。
有没有人有任何想法吗?
谢谢。
--update -
Django的版本是1.3
imports in the profile/models.py
from django.contrib.auth.models import User
from django.db.models.signals import post_save, pre_save
import logging
从admin.py 进口django.contrib中导入管理 从django.contrib.auth.models从导入 集团test.django.registration.models从test.django.registration.admin导入注册配置文件 从django.contrib.auth.admin导入RegistrationAdmin 从django.contrib.auth.model导入UserAdmin,GroupAdmin 从test.django.extranet.profile.models的进口用户 导入用户配置 从test.django.common.models.ajrsonline.ajrsonline_users汇入test.django.common.middleware.ajrs_ldap进口AJRSOnlineLdapBackend
什么版本的Django,你可以发布你的导入? – 2013-02-24 21:49:43
只是以防万一:http://stackoverflow.com/questions/2345400/why-is-post-save-being-raised-twice-during-the-save-of-a-django-model – 2013-02-24 21:52:14
格林,谢谢你的响应,我已经检查了问题2345400,并且更改已安装应用的条目并没有改变任何内容。 – grantk 2013-02-24 22:16:58