0

phoneUserProfile. 我怎样才能让它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。

4

1 回答 1

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这里

于 2013-07-23T19:23:17.030 回答