Django Admin:如何在同一视图中显示两个不同模型的字段?

发布于 2024-09-13 02:37:47 字数 748 浏览 2 评论 0原文

我的网站使用 Django 的用户身份验证用户模型和自定义 UserProfile 模型来存储一些附加数据(生日等)。有没有办法在 Django 管理中创建一个视图,将 User 和 UserProfile 模型中的字段编织在一起?

我怀疑这个代码片段还不够接近,但也许它有助于说明我正在尝试做的事情:

from django.contrib import admin
from django.contrib.auth.models import User
from userprofile.models import UserProfile


class UserProfileAdmin(admin.ModelAdmin):
    list_display = ('name', 'gender', 'User.email') #user.email creates the error - tried some variations here, but no luck.

admin.site.register(UserProfile, UserProfileAdmin)

错误消息:

配置不正确:UserProfileAdmin.list_display[2],“User.email”不是“UserProfileAdmin”的可调用属性或属性,也不是在模型“UserProfile”中找到的。

最终,我试图创建一个具有first & 的管理视图。用户个人资料中的姓氏和用户的电子邮件。

My site makes use of Django's User Authentication User model and a custom UserProfile model to store some additional data (birthday, etc.). Is there a way to create a view in Django admin that weaves together fields from both the User and UserProfile models?

I suspect that this code snippet is not even close, but maybe it will help illustrate what I'm trying to do:

from django.contrib import admin
from django.contrib.auth.models import User
from userprofile.models import UserProfile


class UserProfileAdmin(admin.ModelAdmin):
    list_display = ('name', 'gender', 'User.email') #user.email creates the error - tried some variations here, but no luck.

admin.site.register(UserProfile, UserProfileAdmin)

Error message:

ImproperlyConfigured: UserProfileAdmin.list_display[2], 'User.email' is not a callable or an attribute of 'UserProfileAdmin' or found in the model 'UserProfile'.

Ultimately, I'm trying to create an admin view that has first & last name from UserProfile and email from User.

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

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

发布评论

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

评论(3

暗地喜欢 2024-09-20 02:37:47

要显示用户电子邮件,您需要在 UserProfileUserProfileAdmin 上有一个方法,该方法返回

UserProfile

def user_email(self):
    return self.user.email

或 UserProfileAdmin

def user_email(self, instance):
    return instance.user.email

上的电子邮件,然后将您的 list_display 更改为

list_display = ('name', 'gender', 'user_email')

“相关”文档: ModelAdmin.list_display

for displaying user email you need to have a method on UserProfile or UserProfileAdmin that returns the email

on UserProfile

def user_email(self):
    return self.user.email

or on UserProfileAdmin

def user_email(self, instance):
    return instance.user.email

then change your list_display to

list_display = ('name', 'gender', 'user_email')

Related docs: ModelAdmin.list_display

猫腻 2024-09-20 02:37:47

您可以尝试使用 InlineModelAdmin 显示 User 和管理视图中的 UserPofile 表单。

要在更改列表中显示用户配置文件信息,您可以创建一个新方法,将值从 UserProfile 委托给 User 模型。

例如,这或多或少应该有效:)

from django.contrib import admin
from django.contrib.auth.models import User

from my_models import UserProfile

class UserProfileInline(admin.StackedInline):
    model = UserProfile
    fk_name = 'user'

class UserAdmin(admin.ModelAdmin):
    list_display = ['get_userprofile_name', 'email']
    list_select_related = True
    inlines = [
        UserProfileInline,
    ]

    def get_userprofile_name(self, instance):
        # instance is User instance
        return instance.get_profile().name

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

You could try using InlineModelAdmin to display both User and UserPofile forms in a admin view.

To display user profile information in change list you can create a new method that delegates the values from UserProfile to User model.

For example this should work more or less :)

from django.contrib import admin
from django.contrib.auth.models import User

from my_models import UserProfile

class UserProfileInline(admin.StackedInline):
    model = UserProfile
    fk_name = 'user'

class UserAdmin(admin.ModelAdmin):
    list_display = ['get_userprofile_name', 'email']
    list_select_related = True
    inlines = [
        UserProfileInline,
    ]

    def get_userprofile_name(self, instance):
        # instance is User instance
        return instance.get_profile().name

admin.site.unregister(User)
admin.site.register(User, UserAdmin)
林空鹿饮溪 2024-09-20 02:37:47

使用 Ashoks 的最佳答案,我制作了一个片段,简化了大量字段的此过程

class ColumnViewer(object):
    pass

column_list = ('name', 'surname', )

for col in column_list:
    setattr(ColumnViewer, col, lambda s,i : getattr(i, col))

@admin.register(UserProfile)
class UserProfileAdmin(admin.ModelAdmin, ColumnViewer):
    list_display = column_list

Using Ashoks top answer i made snippet that simplifies this process for large number of fields

class ColumnViewer(object):
    pass

column_list = ('name', 'surname', )

for col in column_list:
    setattr(ColumnViewer, col, lambda s,i : getattr(i, col))

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