[Answered ]-How to access attributes of the 'User' model extended by another model referenced by a OnetoOneField in admin.py

1👍

You can define methods and reference these in the list_display:

class TutorAdmin(admin.ModelAdmin):
    list_display = ('user_id', 'contact_number', username)

    @admin.display(description='username')
    def username(self, obj):
        u = obj.user
        if u is not None:
            return u.username

You can however not edit the usernames, since the function defines how to access the data, not how to alter it.

Since the __str__ for the builtin User model returns the username, you can also work with:

class TutorAdmin(admin.ModelAdmin):
    list_display = ('user_id', 'contact_number', user)

Leave a comment