I could not find a way of showing the customized user's custom required fields on the 'add new user page' of admin.
I have created a custom user that extends AbstractUser and added three required custom fields. I did not create custom UserManager because i am extending from AbstractUser not AbstractBaseUser.
For admin side: 1. I have created a custom UserCreationForm by extending it. Inside the meta class i added those new three custom fields
But i can not see the custom fields on the admin side. Am i doing smt wrong?
Here is the code for admin side:
class MyUserCreationForm(UserCreationForm):
"""A form for creating new users. Includes all the required
fields, plus a repeated password."""
password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput)
class Meta:
model = get_user_model()
fields = ('customField1', 'customField2', 'customField3',)
def clean_password2(self):
# Check that the two password entries match
password1 = self.cleaned_data.get("password1")
password2 = self.cleaned_data.get("password2")
if password1 and password2 and password1 != password2:
raise forms.ValidationError("Passwords don't match")
return password2
def save(self, commit=True):
# Save the provided password in hashed format
user = super(UserCreationForm, self).save(commit=False)
user.set_password(self.cleaned_data["password1"])
if commit:
user.save()
return user
class MyUserAdmin(UserAdmin):
form = MyUserChangeForm
add_form = MyUserCreationForm
fieldsets = (
(None, {'fields': [('username', 'password', 'customField1', 'customField2', 'customField3'),]}),
(_('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')}),
)
admin.site.register( CustomUser, MyUserAdmin)
SOLUTION --- Adding 'add_fieldsets' to extended UserAdmin class makes the fields appear.
add_fieldsets = ( (None, { 'classes': ('wide',), 'fields': ('username', 'password1', 'password2', 'customField1', 'customField2', 'customField3', )} ),
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With