我的phone
中有UserProfile
字段。如何将它显示在Django Admin › Auth › Users
列表中,从外部(列表显示) - 而不是记录内部?
我现在有:
class UserAdmin(UserAdmin):
list_display = ('email', 'first_name', 'last_name', 'userprofile__phone')
inlines = (UserProfileInline,)
# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
userprofile__phone无法识别。
答案 0 :(得分:6)
一种方法是
class UserAdmin(UserAdmin):
list_display = ('email', 'first_name', 'last_name', 'phone')
inlines = (UserProfileInline,)
def phone(self, obj):
try:
phone = obj.userprofile.phone #Or change this to how you would access the userprofile object - This was assuming that the User, Profile relationship is OneToOne
return phone
except:
return ""
phone.short_description = 'Phone'
# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
的更多详情