Custom User model fields (AbstractUser) not showing in django admin

前端 未结 3 1807
独厮守ぢ
独厮守ぢ 2021-02-05 10:00

I have extended User model for django, using AbstractUser method. The problem is, my custom fields do not show in django admin panel.

My models.py:

from          


        
3条回答
  •  爱一瞬间的悲伤
    2021-02-05 10:29

    If all you want to do is add new fields to the standard edit form (not creation), there's a simpler solution than the one presented above.

    from django.contrib import admin
    from django.contrib.auth.admin import UserAdmin
    
    from .models import User
    
    
    class CustomUserAdmin(UserAdmin):
        fieldsets = (
            *UserAdmin.fieldsets,  # original form fieldsets, expanded
            (                      # new fieldset added on to the bottom
                'Custom Field Heading',  # group heading of your choice; set to None for a blank space instead of a header
                {
                    'fields': (
                        'is_bot_flag',
                    ),
                },
            ),
        )
    
    
    admin.site.register(User, CustomUserAdmin)
    

    This takes the base fieldsets, expands them, and adds the new one to the bottom of the form. You can also use the new CustomUserAdmin class to alter other properties of the model admin, like list_display, list_filter, or filter_horizontal. The same expand-append method applies.

提交回复
热议问题