Django - User, UserProfile, and Admin

后端 未结 3 2023
执念已碎
执念已碎 2020-12-04 18:29

I\'m trying to get the Django Admin interface to display information about my profile. It displays all of my users but no profile information. I\'m not quite sure how to g

3条回答
  •  有刺的猬
    2020-12-04 19:01

    This is not exactly an answer to your question BUT, according to Django Admin documentation, you can display information from UserProfile in your User "table". And you can make it searchable.

    That would look something like this (modifying answer from C. Alan Zoppa):

    class UserProfileAdmin(UserAdmin):
        inlines = [ UserProfileInline, ]
        def company(self, obj):
            try:
                return obj.get_profile().company
            except UserProfile.DoesNotExist:
                return ''
        list_display = UserAdmin.list_display + ('company',)
        search_fields = UserAdmin.search_fields + ('userprofile__company',)
    

    You might have an issue though with search if your profile class is no longer called UserProfile.

提交回复
热议问题