自定义 Django 用户模型时使用新的管理表单

发布于 2024-11-26 01:10:05 字数 1430 浏览 4 评论 0原文

我正在扩展用户模型,但似乎在使用新的管理表单时遇到问题。 我在 models.py 中有以下代码:

    class Preference(models.Model):
        choice = models.TextField(choices = (('grid', 'grid'), ('list','list'))) 

        def __unicode__(self):
            return self.choice

    class UserProfile2(models.Model):
        preference = models.ForeignKey(Preference, default = Preference.objects.get(id=2).id)
        user = models.OneToOneField(User, unique=True)

    def create_user_profile(sender, instance, created, **kwargs):
        if created:
            print 'creating user profile2'
            u = UserProfile2.objects.create(user=instance)   

    post_save.connect(create_user_profile, sender=User)

以下代码在 admin.py 中:

    class UserProfileInline(admin.TabularInline):
        model = UserProfile2
        fk_name = 'user'

    class CustomUserAdmin(UserAdmin):
        inlines = [UserProfileInline,]

    admin.site.unregister(User)
    admin.site.register(User, CustomUserAdmin)     

以下代码在 settings.py 中:

    AUTH_PROFILE_MODULE = 'userextension.UserProfile2' 

当用户不尝试控制管理中首选项对象的值并创建一个新用户使用默认值。但是,如果用户尝试放弃“列表”的默认值并改用“网格”,我会收到“密钥 user_id 的重复条目”错误。

我是否需要显式从管理表单中获取额外字段的值并保存 userprofile2 对象和用户对象?如果是这样,这与我收到的错误有什么关系?我还没有找到太多关于如何做到这一点的文档,并且非常感谢任何指导。

更新: 这似乎也很重要:当我删除 UserProfile2 模型中首选项的默认值时,我得到的错误是“列'preference_id'不能为空”

感谢您查看我的问题。

I am extending the User Model, but seem to be having a problem with using my new admin form.
I have the following code in models.py:

    class Preference(models.Model):
        choice = models.TextField(choices = (('grid', 'grid'), ('list','list'))) 

        def __unicode__(self):
            return self.choice

    class UserProfile2(models.Model):
        preference = models.ForeignKey(Preference, default = Preference.objects.get(id=2).id)
        user = models.OneToOneField(User, unique=True)

    def create_user_profile(sender, instance, created, **kwargs):
        if created:
            print 'creating user profile2'
            u = UserProfile2.objects.create(user=instance)   

    post_save.connect(create_user_profile, sender=User)

The following code is in admin.py:

    class UserProfileInline(admin.TabularInline):
        model = UserProfile2
        fk_name = 'user'

    class CustomUserAdmin(UserAdmin):
        inlines = [UserProfileInline,]

    admin.site.unregister(User)
    admin.site.register(User, CustomUserAdmin)     

The following is in settings.py:

    AUTH_PROFILE_MODULE = 'userextension.UserProfile2' 

It works correctly when the user does not try to control the value for preference object in the admin, and creates a new user using the default value. But if the user tries to switch away from the default value of 'list' and uses 'grid' instead, I get a 'Duplicate Entry for Key user_id' error.

Do I need to explicitly get the value from the admin form for the extra field and save both the userprofile2 object and the user object? If so, how is that related to the error I receive? I haven't found much documentation for how to do that, and would much appreciate any direction.

Update:
This seems important, too: When I remove the default value for preference in the UserProfile2 model, the error I get is "Column 'preference_id' cannot be null"

Thanks for taking a look at my question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

久伴你 2024-12-03 01:10:05

您的信号根据“created”的值在 User 对象的 post_save 上创建一个新的 UserProfile2 对象。我敢打赌,每次调用此信号时,“created”都是 True,并且每次您在 admin 中保存有关 User 对象的任何信息时,它都会尝试创建一个新的 UserProfile2 对象。

UserProfile2 的用户外键设置为唯一,这就是您收到重复键错误的地方。

尝试将信号代码更改为:

def create_user_profile(sender, instance, created, **kwargs):
        if created:
            u = UserProfile2.objects.get_or_create(user=instance)

应该防止重复键错误。希望对您有所帮助。

Your signal creates a new UserProfile2 object on post_save of a User object, based on the value of 'created'. I'm betting that 'created' is True every time this signal is called, and it's trying to create a new UserProfile2 object each time you save any information on a User object in admin.

UserProfile2's user foreign key is set to unique, which is where you're getting the duplicate key error.

Try changing your signal code to:

def create_user_profile(sender, instance, created, **kwargs):
        if created:
            u = UserProfile2.objects.get_or_create(user=instance)

that should prevent the duplicate key error. Hope that helps you out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文