[英]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. 因此,我的目标是扩展User以添加一些自定义字段。
In models.py I have added the following 在models.py中,我添加了以下内容
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: 在admin.py中,我添加了以下内容:
class CustomUserAdmin(UserAdmin):
model = MyUser
filter_horizontal = ('user_permissions', 'groups',)
admin.site.register(MyUser, CustomUserAdmin)
In settings.py I have the following: 在settings.py中,我具有以下内容:
AUTH_USER_MODEL = 'app_name.MyUser'
In urls.py: 在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 app_name位于settings.py中的INSTALLED_APPS中
I have also tried to add in form.py custom Creation and Change form like follows, without success: 我也曾尝试在form.py中添加如下的自定义创建和更改表单,但未成功:
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. 文档建议对默认的User
模型使用OneToOne
关系来扩展它。 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. 您已经从头创建了一个新的用户模型( 单击 ),但是似乎遗漏了很多必需的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.