简体   繁体   English

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

[英]Django Admin: how to display fields from two different models in same view?

My site makes use of Django's User Authentication User model and a custom UserProfile model to store some additional data (birthday, etc.). 我的网站使用Django的用户身份验证用户模型和自定义UserProfile模型来存储一些额外的数据(生日等)。 Is there a way to create a view in Django admin that weaves together fields from both the User and UserProfile models? 有没有办法在Django管理员中创建一个视图,将来自User和UserProfile模型的字段编织在一起?

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'. NotperlyConfigured:UserProfileAdmin.list_display [2],'User.email'不是'UserProfileAdmin'的可调用或属性,或者在模型'UserProfile'中找到。

Ultimately, I'm trying to create an admin view that has first & last name from UserProfile and email from User. 最终,我正在尝试创建一个管理视图,其中包含来自UserProfile的姓名和来自User的电子邮件。

for displaying user email you need to have a method on UserProfile or UserProfileAdmin that returns the email 要显示用户电子邮件,您需要在UserProfileUserProfileAdmin上使用返回电子邮件的方法

on UserProfile 在UserProfile上

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

or on UserProfileAdmin 或者在UserProfileAdmin上

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

then change your list_display to 然后将list_display更改为

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

Related docs: ModelAdmin.list_display 相关文档: ModelAdmin.list_display

You could try using InlineModelAdmin to display both User and UserPofile forms in a admin view. 您可以尝试使用InlineModelAdmin在管理视图中显示User和UserPofile表单。

To display user profile information in change list you can create a new method that delegates the values from UserProfile to User model. 要在更改列表中显示用户配置文件信息,您可以创建一个新方法,将值从UserProfile委派给User模型。

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)

Using Ashoks top answer i made snippet that simplifies this process for large number of fields 使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM