简体   繁体   中英

Colum not showing in admin panel

Implementing custom user for my project,
here is my account/models.py

class myUser(AbstractBaseUser, PermissionsMixin):
    #blah
    date_joined = models.DateTimeField(auto_now_add=True)
    #blah

and my account/admin.py

class myUserDetail(admin.ModelAdmin):
    list_display = ('email','password','is_active','is_staff','date_joined',)
    fields = list_display    

admin.site.register(myUser, myUserDetail)

The list_display works fine, but when I click into a user, error is raised : Unknown field(s) (date_joined) specified for myUser. Check fields/fieldsets/exclude attributes of class myUserDetail. Unknown field(s) (date_joined) specified for myUser. Check fields/fieldsets/exclude attributes of class myUserDetail.

In fact it exists in postgres...
Please help!

The error is being triggered when the ModelForm is created automatically for the admin, specifically if there are missing fields . Because you are using auto_now_add=True , which implicitly sets editable=False , the field cannot be included in the automatically generated form. Because of this, an error is triggered.

I would recommend specifying fields and list_display independently, as they aren't actually the same.

When you use a custom User model in Django you should add the following line to your settings:

AUTH_USER_MODEL = 'accounts.MyUser'

I also think you should subclass from AbstractUser which is an abstract base class implementing a fully featured User model with admin-compliant permissions, and includes email , date_joined , etc. fields.

You can read more about customizing the user model in the Django documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM