2013-02-07 4 views
6

Я не смог найти способ отображения пользовательских обязательных полей пользовательского интерфейса на странице «Добавить новую пользовательскую страницу» администратора.Django 1.5 rc1 пользовательская форма создания пользователя с настраиваемыми полями

Я создал пользовательский пользователь, который расширяет AbstractUser и добавил три обязательных настраиваемых поля. Я не создал пользовательский UserManager, потому что я расширяюсь от AbstractUser not AbstractBaseUser.

Для администратора: 1. Я создал пользовательскую форму UserCreationForm, расширив ее. Внутри meta-класса я добавил эти новые три пользовательских поля

Но я не вижу пользовательские поля на стороне администратора. Я делаю smt неправильно?

Вот код для стороны администратора:

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) 
+4

РЕШЕНИЕ --- Добавление дополнений 'add_fieldsets' к расширенному классу UserAdmin приводит к появлению полей. add_fieldsets = ( (None, { «classes»: ('wide',), 'fields': ('username', 'password1', 'password2', 'customField1', 'customField2', 'customField3', 'customField3')} ), – ratata

+0

Hey @ratata Можете ли вы разместить свое решение в качестве ответа, чтобы мы могли его разрешить, как ответ !? – Azd325

ответ

4

РЕШЕНИЕ --- Добавление «add_fieldsets» в расширенном классе UserAdmin делает появляются поля.

add_fieldsets = ((None, { 'classes': ('wide',), 'fields': ('username', 'password1', 'password2', 'customField1', 'customField2', 'customField3',)}), 
Смежные вопросы