如何在管理界面中内联编辑 django 用户配置文件?

发布于 2024-09-13 09:46:34 字数 636 浏览 8 评论 0原文

如果你想在 Django 中存储有关用户 (django.contrib.auth.models.User) 的额外信息,你可以使用漂亮的 AUTH_PROFILE_MODULE 来插入“配置文件”模型。然后每个用户都会获得一个个人资料。全部描述如下:

现在,假设我已经创建了一个名为 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:

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 技术交流群。

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

发布评论

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

评论(4

囚我心虐我身 2024-09-20 09:46:34

我建议对 André 的解决方案进行稍微改进,因为它破坏了 /admin/auth/user/ 中的列表视图:

from django.contrib import admin
from member.models import UserProfile
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin as AuthUserAdmin

class UserProfileInline(admin.StackedInline):
 model = UserProfile
 max_num = 1
 can_delete = False

class UserAdmin(AuthUserAdmin):
 inlines = [UserProfileInline]

# unregister old user admin
admin.site.unregister(User)
# register new user admin
admin.site.register(User, UserAdmin)

I propse a slightly improved version of André's solution as it breaks the list view in /admin/auth/user/:

from django.contrib import admin
from member.models import UserProfile
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin as AuthUserAdmin

class UserProfileInline(admin.StackedInline):
 model = UserProfile
 max_num = 1
 can_delete = False

class UserAdmin(AuthUserAdmin):
 inlines = [UserProfileInline]

# unregister old user admin
admin.site.unregister(User)
# register new user admin
admin.site.register(User, UserAdmin)
困倦 2024-09-20 09:46:34

好吧,事实证明这很容易,只要你知道如何去做。这是我的 myapp/accounts/admin.py:

from django.contrib import admin
from myapp.accounts.models import UserProfile
from django.contrib.auth.models import User

class UserProfileInline(admin.StackedInline):
    model = UserProfile
    max_num = 1
    can_delete = False

class AccountsUserAdmin(admin.UserAdmin):
    inlines = [UserProfileInline]

# unregister old user admin
admin.site.unregister(User)
# register new user admin that includes a UserProfile
admin.site.register(User, AccountsUserAdmin)

用户的默认 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:

from django.contrib import admin
from myapp.accounts.models import UserProfile
from django.contrib.auth.models import User

class UserProfileInline(admin.StackedInline):
    model = UserProfile
    max_num = 1
    can_delete = False

class AccountsUserAdmin(admin.UserAdmin):
    inlines = [UserProfileInline]

# unregister old user admin
admin.site.unregister(User)
# register new user admin that includes a UserProfile
admin.site.register(User, AccountsUserAdmin)

The default admin.UserAdmin ModelAdmin class for users is unregistered and a new one specifying an inline UserProfile is registered in its place. Just thought I should share.

友谊不毕业 2024-09-20 09:46:34

我建议对 Robert 的解决方案进行另一项改进:

from django.contrib import admin
from member.models import UserProfile
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin as AuthUserAdmin

class UserProfileInline(admin.StackedInline):
   model = UserProfile
   max_num = 1
   can_delete = False

class UserAdmin(AuthUserAdmin):
   def add_view(self, *args, **kwargs):
      self.inlines = []
      return super(UserAdmin, self).add_view(*args, **kwargs)

   def change_view(self, *args, **kwargs):
      self.inlines = [UserProfileInline]
      return super(UserAdmin, self).change_view(*args, **kwargs)

# unregister old user admin
admin.site.unregister(User)
# register new user admin
admin.site.register(User, UserAdmin)

如果不对 UserAdmin 进行此更改,自定义 UserProfileInline 部分将显示在“添加用户”屏幕上,该屏幕只是要求输入用户名和密码。如果您在保存之前更改该屏幕上的任何配置文件数据(远离默认值),您将收到“重复密钥”数据库错误。

I propose another improvement to Robert's solution:

from django.contrib import admin
from member.models import UserProfile
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin as AuthUserAdmin

class UserProfileInline(admin.StackedInline):
   model = UserProfile
   max_num = 1
   can_delete = False

class UserAdmin(AuthUserAdmin):
   def add_view(self, *args, **kwargs):
      self.inlines = []
      return super(UserAdmin, self).add_view(*args, **kwargs)

   def change_view(self, *args, **kwargs):
      self.inlines = [UserProfileInline]
      return super(UserAdmin, self).change_view(*args, **kwargs)

# unregister old user admin
admin.site.unregister(User)
# register new user admin
admin.site.register(User, UserAdmin)

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.

兔小萌 2024-09-20 09:46:34

您必须考虑添加和更改表单。否则,在尝试创建用户时,您将收到“用户不能为无”错误。以下内容已经过测试并可在 1.3 中运行:

class TeamInline(admin.StackedInline):
  model = Team
  fk_name = 'user'
  max_num = 1
  can_delete = False

class TeamUserAdmin(UserAdmin):
  list_display = ('username', 'email', 'company', 'expertise', 'contact_email', 'contact_phone', 'twitter', 'facebook', 'last_login_short', 'options')
  list_select_related = True

  def add_view(self, *args, **kwargs):
    self.inline_instances = []
    return super(TeamUserAdmin, self).add_view(*args, **kwargs)

  def change_view(self, *args, **kwargs):
    self.inline_instances.append(TeamInline(self.model, self.admin_site))
    return super(TeamUserAdmin, self).change_view(*args, **kwargs)

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:

class TeamInline(admin.StackedInline):
  model = Team
  fk_name = 'user'
  max_num = 1
  can_delete = False

class TeamUserAdmin(UserAdmin):
  list_display = ('username', 'email', 'company', 'expertise', 'contact_email', 'contact_phone', 'twitter', 'facebook', 'last_login_short', 'options')
  list_select_related = True

  def add_view(self, *args, **kwargs):
    self.inline_instances = []
    return super(TeamUserAdmin, self).add_view(*args, **kwargs)

  def change_view(self, *args, **kwargs):
    self.inline_instances.append(TeamInline(self.model, self.admin_site))
    return super(TeamUserAdmin, self).change_view(*args, **kwargs)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文