Django 1.5 rc1 user creation form with custom fields

I couldn’t find a way to display the custom required custom fields on the "Add New Custom Page" page of the administrator.

I created a user user that extends AbstractUser and added three required custom fields. I did not create a custom UserManager because I am expanding from AbstractUser not AbstractBaseUser.

For the administrator: 1. I created a custom UserCreationForm, expanding it. Inside the metaclass, I added these three new custom fields

But I can not see user fields on the admin side. Am I doing smt wrong?

Here is the code for admin:

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) 
+6
source share
1 answer

SOLUTION: adding add_fieldsets fields to the extended UserAdmin class results in fields.

 add_fieldsets = ( (None, { 'classes': ('wide',), 'fields': ('username', 'password1', 'password2', 'customField1', 'customField2', 'customField3', )} ), 
+4
source

All Articles