Reputation: 41
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 there one additional field (for testing) and planning to add more. Somehow I managed to making it work with DjangoAdmin using UserChangeForm with Meta class.
Problem: According to Django-docs there is no need to do anything more to get possibility to see, edit and add value to every field in my DjangoAdmin site. Unfortunately - Seeing and editing is working as expected but when it comes to "Add new User" by DjangoAdmin - I still see only three fields: "Username", "Password" and "Password confirmation".
Question: Where should I edit/put some code to get possibility to fill more fields while I`m creating new user by DjangoAdmin?
Ending: Yes - I've tried a lot of google results (including using 'fieldsets', 'list_display', 'inline' methods, etc.. As much, as possible - I would like also to avoid using OneToOne relation with User model. That generated so many mistakes that I gave up on it and I don't want to be back there. If there is such a need - I'll provide code snippets.
Thanks for help in advance.
Upvotes: 0
Views: 900
Reputation: 41
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
class NewUserManager(UserManager):
def create_user(self, username, email=None, password=None, **extra_fields):
return UserManager.create_user(self, username, email=email, password=password, **extra_fields)
def create_superuser(self, username, email, password, **extra_fields):
return UserManager.create_superuser(self, username, email, password, **extra_fields)
class NewUser(AbstractUser):
newField = models.CharField() # for example CharField
newField = models.CharField(max_length = 12) # important thing - properties should be in the second line
objects = NewUserManager()
class Meta(AbstractUser.Meta):
swappable = 'your_app_name.NewUser'
#db_table = 'auth_user'
forms.py
class NewUserCreationForm(forms.ModelForm):
password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput)
class Meta:
model = NewUser
fields = ('username', 'email', 'first_name', 'last_name', 'newField') # few example fields and new one: newField
def clean_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
def save(self, commit=True):
user = super(NewUserCreationForm, self).save(commit=False)
user.set_password(self.cleaned_data["password1"])
if commit:
user.save()
return user
class NewUserChangeForm(UserChangeForm):
class Meta(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
class NewUserAdmin(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)
Upvotes: 2