在列表外的Django Admin中显示UserProfile字段

时间:2013-07-23 18:53:16

标签: python django django-admin

我的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无法识别。

1 个答案:

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

有关list_display here

的更多详情