如何在Django中的自定义用户配置文件中显示姓氏

时间:2018-01-14 07:16:09

标签: python django django-admin

我需要在用户个人资料中显示所有用户的姓氏,但我只能显示用户名。

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)

enter image description here

2 个答案:

答案 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