I have added the field introduction
to my CustomUser
model and made the proper migrations:
class CustomUser(AbstractUser):
introduction = models.CharField(max_length=5000, null=True, blank=True, default="")
def get_absolute_url(self):
return reverse('user_detail', args=[str(self.username)])
admin.py
overrides the default admin for users:
CustomUser = get_user_model()
class CustomUserAdmin(UserAdmin):
add_form = CustomUserCreationForm
form = CustomUserChangeForm
model = CustomUser
list_display = ['username', 'introduction',]
admin.site.register(CustomUser,CustomUserAdmin)
forms.py
:
class CustomUserCreationForm(UserCreationForm):
class Meta:
model = get_user_model()
fields = ('username',)
class CustomUserChangeForm(UserChangeForm):
class Meta:
model = get_user_model()
fields = ('username', 'introduction',)
Despite this, when I try to modify a user in the Django admin, the introduction
field doesn't appear.
I am using django 3.0.1.
You need to add introduction field name in fieldsets list of CustomUserAdmin because it is already implemented in the base class which is the reason of not showing the new field in django admin automatically.
According to django documentation:
fieldsets is a list of two-tuples, in which each two-tuple represents a on the admin form page. (A is a “section” of the form.)
The two-tuples are in the format (name, field_options), where name is a string representing the title of the fieldset and field_options is a dictionary of information about the fieldset, including a list of fields to be displayed in it.
I got fieldsets from AbstractUser class and extended by introduction field. This code snippet must help you to show new user model field in admin:
from django.utils.translation import gettext, gettext_lazy as _
class CustomUserAdmin(UserAdmin):
add_form = CustomUserCreationForm
form = CustomUserChangeForm
model = CustomUser
list_display = ['username', 'introduction',]
fieldsets = (
(None, {'fields': ('username', 'password')}),
(_('Personal info'), {'fields': ('first_name', 'last_name', 'email', 'introduction')}),
(_('Permissions'), {
'fields': ('is_active', 'is_staff', 'is_superuser', 'groups', 'user_permissions'),
}),
(_('Important dates'), {'fields': ('last_login', 'date_joined')}),
)
I don't know if this is the ideal way to do it,but what I did was making changes in the field_sets of UserAdmin like the following:
class CustomUserAdmin(UserAdmin):
fieldsets = (
(None, {'fields': ('username', 'password','introduction')}),
(_('Personal info'), {'fields': ('first_name', 'last_name', 'email')}),
(_('Permissions'), {
'fields': ('is_active', 'is_staff', 'is_superuser', 'groups', 'user_permissions'),
}),
(_('Important dates'), {'fields': ('last_login', 'date_joined')}),
)
add_form = CustomUserCreationForm
form = CustomUserChangeForm
model = CustomUser
I just added introduction to the fields,and it appears on the admin site
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.