4

I want to add my settings.AUTH_USER_MODEL to my admin insted of User model. I register it with the snippet I found in the docs:

class UserAdmin(BaseUserAdmin):
    # The forms to add and change user instances
    form = UserChangeForm
    add_form = UserCreationForm

    # The fields to be used in displaying the User model.
    # These override the definitions on the base UserAdmin
    # that reference specific fields on auth.User.
    list_display = ('email', 'bdate')
    fieldsets = (
        (None, {'fields': ('email', 'password')}),
        ('Personal info', {'fields': ('bdate', 'website', 'location')}),
    )
    # add_fieldsets is not a standard ModelAdmin attribute. UserAdmin
    # overrides get_fieldsets to use this attribute when creating a user.
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('email', 'password1', 'password2')}
        ),
    )
    search_fields = ('email',)
    ordering = ('email',)
    filter_horizontal = ()

In fieldsets Personal info I added all my custom info. Now I also want to display all inherited fields like first_name, username, etc. I can add them one by one to fieldsets, but I am not sure if that's the right way.

Is there a way just to inherit them from User model without specifying explicitly?

1 Answer 1

5

You could make use of ModelAdmin.get_fieldsets():

class UserAdmin(BaseUserAdmin):
    def get_fieldsets(self, request, obj=None):
        fieldsets = list(super(UserAdmin, self).get_fieldsets(request, obj))
        # update the `fieldsets` with your specific fields
        fieldsets.append(
            ('Personal info', {'fields': ('bdate', 'website', 'location')}))
        return fieldsets
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect, that's what I needed. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.