我需要在用户个人资料中显示所有用户的姓氏,但我只能显示用户名。
Admin.py
from django.contrib import admin
from commonpage.models import UserProfile,PositionTable
#register the above models
class user_action(admin.ModelAdmin):
list_display = ['user', 'useractive','position','team_name','preferd_shift']
ordering = ['id']
list_filter = ('useractive','position','team_name','preferd_shift')
list_editable = ['position','team_name','preferd_shift']
actions = [make_user_active,make_user_deactive]
# Register your models here.
admin.site.register(UserProfile,user_action)
admin.site.register(PositionTable)
答案 0 :(得分:0)
使用__
(下划线) + field_name
轻松完成此操作。
list_display = ['user', 'user__last_name', 'useractive','position','team_name','preferd_shift']
答案 1 :(得分:0)
您无法直接在list_display
中使用相关字段,但您可以将其指向返回相关字段的方法:
class user_action(admin.ModelAdmin):
list_display = ['user', 'last_name', ...]
...
def last_name(self, obj):
return obj.user.last_name