New Field In Django-admin Add User
I'm using Django 1.7.1 and here I encountered issue that shouldn`t really took place. Background: I'm working on customized user model based on AbstractUser. For now - I've got the
Solution 1:
I'm not sure if the problem was with specific lines order in NewUserAdmin class or something else - haven`t tested it yet, but I'm pretty sure I was close to this solution much earlier. However this is the exact thing I was talking about (it is working the way I wanted it to :) ). I can now easy add any field to user model and it is still connected to all framework functions like sessions management, and so on. What is more - I don't use AbstractBaseUser nor OneToOne field, which caused a lot of problems. Anyway - thanks for any way of help. I wouldn't resolve it without you... ...this week :D
PS I skipped all imports. IDE will tell you, what is needed.
models.py
classNewUserManager(UserManager):
defcreate_user(self, username, email=None, password=None, **extra_fields):
return UserManager.create_user(self, username, email=email, password=password, **extra_fields)
defcreate_superuser(self, username, email, password, **extra_fields):
return UserManager.create_superuser(self, username, email, password, **extra_fields)
classNewUser(AbstractUser):
newField = models.CharField() # for example CharField
newField = models.CharField(max_length = 12) # important thing - properties should be in the second line
objects = NewUserManager()
classMeta(AbstractUser.Meta):
swappable = 'your_app_name.NewUser'#db_table = 'auth_user'
forms.py
classNewUserCreationForm(forms.ModelForm):
password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput)
classMeta:
model = NewUser
fields = ('username', 'email', 'first_name', 'last_name', 'newField') # few example fields and new one: newFielddefclean_password2(self):
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
defsave(self, commit=True):
user = super(NewUserCreationForm, self).save(commit=False)
user.set_password(self.cleaned_data["password1"])
if commit:
user.save()
return user
classNewUserChangeForm(UserChangeForm):
classMeta(UserChangeForm.Meta):
model = NewUser
fields = ('username', 'email', 'first_name', 'last_name', 'newField', 'last_login', 'is_staff') # few example fields and new one: newField
admin.py
classNewUserAdmin(UserAdmin):
form = NewUserChangeForm
add_form = NewUserCreationForm
list_display = ('username', 'newField', 'first_name', 'last_name', 'last_login',) # few example fields and new one: newField
fieldsets = (
(None, {'fields': ('username', 'email', 'first_name', 'last_name', 'newField', 'last_login', 'is_staff')}), # few example fields and new one: newField
)
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('username', 'newField', 'password1', 'password2')} # here are fields you want to have in django-admin AddUser view
), # one of the most important place above
)
admin.site.register(NewUser, NewUserAdmin)
Post a Comment for "New Field In Django-admin Add User"