Django 将用户配置文件与管理中的配置文件交错

发布于 2024-09-24 07:27:36 字数 562 浏览 4 评论 0原文

我有一个用户个人资料,当前通过堆叠内联显示在管理中。但是,因为我有诸如last_name_prefix和last_name_suffix之类的字段(对于Piet van Dijk之类的外国名字,以涵盖按姓氏进行正确排序),我希望能够将用户配置文件字段与正常的更改用户字段交错。因此,在更改用户管理界面中,它会显示如下:

名字:
姓氏前缀:
姓氏
姓氏后缀:

我尝试过以下解决方案:http:// groups.google.com/group/django-users/browse_thread/thread/bf7f2a0576e4afd1/5e3c1e98c0c2a5b1。但这只是在用户表单中创建了实际上并非来自用户配置文件的额外字段(即使它们应该从用户配置文件中获取值,它们仍然为空)。

有人可以向我解释一下这是否可以做到以及如何做到吗? 非常感谢!

I have a User Profile which is currently shown in the Admin via a Stacked Inline. However because I have fields such as last_name_prefix and last_name_suffix (for foreign names such as Piet van Dijk to cover proper sorting by last name) I would like to be able interleave the user profile fields with the normal change user fields. So in the Change User admin interface it would appear like this:

First Name:
Last Name Prefix:
Last Name
Last Name Suffix:

I have tried this solution: http://groups.google.com/group/django-users/browse_thread/thread/bf7f2a0576e4afd1/5e3c1e98c0c2a5b1. But that just created extra fields in the user form that weren't actually coming from the user profile (they stayed empty even though they should get values from the user profile).

Could someone explain to me if this could be done and how?
Thanks very much!

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

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

发布评论

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

评论(2

赠意 2024-10-01 07:27:36

我很确定您需要覆盖普通用户管理。

我实际上要做的是为 UserProfile 创建一个特殊的 forms.ModelForm ,称为 UserProfileAdminForm ,其中也包含来自 User 模型的字段。然后,您将为管理员注册 UserProfile,并且 UserProfileAdminForm 的 save 函数将捕获用户特定的字段,并创建或更新 User 记录(这被保留为练习OP)。

更多信息

当我说向表单添加更多字段时,我的意思是手动添加它们:

class UserProfileAdminForm(forms.ModelForm):
    username = forms.CharField(...)
    email = forms.EmailField(...)
    first_name = ...
    last_name = ...

    def __init__(self, *args, **kwargs):
        super(UserProfileAdminForm, self).__init__(*args, **kwargs)
        profile = kwargs.get('instance', None)
        if profile and profile.user:
            self.user = profile.user
            self.fields['username'].initial = self.user.username
            self.fields['last_name'].initial = ...
            ...

    class Meta:
        model = UserProfile

I'm pretty sure you'd need to overwrite normal User admin.

What I would actually do is create a special forms.ModelForm for UserProfile called, say UserProfileAdminForm which included fields from the User model as well. Then you'd register UserProfile for admin and the save function for the UserProfileAdminForm would capture the user-specific fields and either create or update the User record (This is left as an exercise to the OP).

More info

When I say add more fields to a form, I mean manually add them:

class UserProfileAdminForm(forms.ModelForm):
    username = forms.CharField(...)
    email = forms.EmailField(...)
    first_name = ...
    last_name = ...

    def __init__(self, *args, **kwargs):
        super(UserProfileAdminForm, self).__init__(*args, **kwargs)
        profile = kwargs.get('instance', None)
        if profile and profile.user:
            self.user = profile.user
            self.fields['username'].initial = self.user.username
            self.fields['last_name'].initial = ...
            ...

    class Meta:
        model = UserProfile
守护在此方 2024-10-01 07:27:36

新的 Django 1.5 版本已经解决了这个问题: https://docs.djangoproject.com/en/1.5/topics/auth/customizing/#auth-custom-user

This question has been solved by the new Django version 1.5: https://docs.djangoproject.com/en/1.5/topics/auth/customizing/#auth-custom-user.

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