For the django admin, how do I add a field to the User model and have it editable in the admin?

后端 未结 5 1259
闹比i
闹比i 2021-02-04 10:42

I\'m trying to understand the django admin better and at the same time, I\'m trying to add one more field to the current user admin. In models.py I\'ve done

Use         


        
5条回答
  •  耶瑟儿~
    2021-02-04 10:59

    First the "is it plugged in?" question -- Have you manually added new_field to the users table in the database? Syncdb wouldn't have taken care of that, of course.

    After that, I would try appending the fields onto the existing UserAdmin rather than rebuilding it from scratch:

    from django.contrib.auth.admin import UserAdmin
    
    UserAdmin.list_display += ('new_field',)  # don't forget the commas
    UserAdmin.list_filter += ('new_field',)
    UserAdmin.fieldsets += ('new_field',)
    

提交回复
热议问题