如何在管理界面中内联编辑 django 用户配置文件?
如果你想在 Django 中存储有关用户 (django.contrib.auth.models.User) 的额外信息,你可以使用漂亮的 AUTH_PROFILE_MODULE 来插入“配置文件”模型。然后每个用户都会获得一个个人资料。全部描述如下:
- http://docs .djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users
- http://www.djangobook.com/en/1.0/chapter12/#cn222
现在,假设我已经创建了一个名为 account 的应用程序,其模型名为 UserProfile,并将其注册为我的用户的个人资料模型。 如何在管理界面中内联编辑个人资料以编辑用户(反之亦然)?
If you want to store extra information about a user (django.contrib.auth.models.User) in Django you can use the nifty AUTH_PROFILE_MODULE to plug in a "profile" model. Each user then gets a profile. It's all described here:
- http://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users
- http://www.djangobook.com/en/1.0/chapter12/#cn222
Now, let's say I have created an application called accounts with a model called UserProfile and registered it as the profile model for my users. How do I inline the editing of the profile in the admin interface for editing users (or vice versa)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我建议对 André 的解决方案进行稍微改进,因为它破坏了 /admin/auth/user/ 中的列表视图:
I propse a slightly improved version of André's solution as it breaks the list view in /admin/auth/user/:
好吧,事实证明这很容易,只要你知道如何去做。这是我的 myapp/accounts/admin.py:
用户的默认
admin.UserAdmin
ModelAdmin 类未注册,并在其位置注册了一个指定内联UserProfile
的新类。只是觉得我应该分享。Well, it turns out that this is quite easy, once you know how to do it. This is my myapp/accounts/admin.py:
The default
admin.UserAdmin
ModelAdmin class for users is unregistered and a new one specifying an inlineUserProfile
is registered in its place. Just thought I should share.我建议对 Robert 的解决方案进行另一项改进:
如果不对 UserAdmin 进行此更改,自定义 UserProfileInline 部分将显示在“添加用户”屏幕上,该屏幕只是要求输入用户名和密码。如果您在保存之前更改该屏幕上的任何配置文件数据(远离默认值),您将收到“重复密钥”数据库错误。
I propose another improvement to Robert's solution:
Without this change to UserAdmin, the custom UserProfileInline section will show up on the "add user" screen, which is just supposed to ask for the username and password. And if you change any of the profile data on that screen (away from the defaults) before you save, you'll get a "duplicate key" database error.
您必须考虑添加和更改表单。否则,在尝试创建用户时,您将收到“用户不能为无”错误。以下内容已经过测试并可在 1.3 中运行:
You have to consider the add and change form. OTherwise you will get a user cannot be None error when trying to create a user. The following has been tested and works in 1.3: