简体   繁体   中英

User custom extend AbstractUser does not appear in 'Authentication' admin panel

I know this problem is common on stack, and I've tried numerous answers Like this one, and more alike .

So my goal is to extend User to add some custom fields.

In models.py I have added the following

class MyUser(AbstractUser):
   bio = models.TextField(max_length=500, blank=True)
   location = models.CharField(max_length=30, blank=True)
   birth_date = models.DateField(null=True, blank=True)

   def __str__(self): # __str__ for Python 3, __unicode__ for Python 2
      return self.name

   class Meta:
      app_label = 'app_name'

In admin.py I have added the following:

class CustomUserAdmin(UserAdmin):
   model = MyUser
   filter_horizontal = ('user_permissions', 'groups',)

admin.site.register(MyUser, CustomUserAdmin)

In settings.py I have the following:

AUTH_USER_MODEL = 'app_name.MyUser'

In urls.py:

admin.autodiscover()

To be noted that I have deleted all my migrations and regenerated them after these changes

app_name is in INSTALLED_APPS in settings.py

I have also tried to add in form.py custom Creation and Change form like follows, without success:

class CustomUserCreationForm(UserCreationForm):
   class Meta(UserCreationForm):
       model = MyUser
       fields = '__all__'
class CustomUserChangeForm(UserChangeForm):
   class Meta:
       model = MyUser
       fields = '__all__'

To be noted that in my database, the user schema is updated with the new field ! 数据库截图

But the problem is that it does not appear in the admin pannel section 管理员面板截图

Thank you in advance for your help

The docs recommend using a OneToOne relation to the default User model to extend it. Try following the docs and see if it works.
You have created a new user model from scratch ( click ), but seem to have left out a lot of required stuff.

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