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 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.