Django 将用户配置文件与管理中的配置文件交错
我有一个用户个人资料,当前通过堆叠内联显示在管理中。但是,因为我有诸如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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我很确定您需要覆盖普通用户管理。
我实际上要做的是为 UserProfile 创建一个特殊的
forms.ModelForm
,称为UserProfileAdminForm
,其中也包含来自User
模型的字段。然后,您将为管理员注册 UserProfile,并且 UserProfileAdminForm 的save
函数将捕获用户特定的字段,并创建或更新User
记录(这被保留为练习OP)。更多信息
当我说向表单添加更多字段时,我的意思是手动添加它们:
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, sayUserProfileAdminForm
which included fields from theUser
model as well. Then you'd register UserProfile for admin and thesave
function for the UserProfileAdminForm would capture the user-specific fields and either create or update theUser
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:
新的 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.