django-admin中的新字段添加用户 [英] New field in django-admin Add User
问题描述
我正在使用Django 1.7.1,在这里我遇到了本不应该真正发生的问题。
背景:我正在基于自定义用户模型进行工作在AbstractUser上。目前-我在那里还有一个字段(用于测试),并计划添加更多字段。
问题:根据Django-docs,不需要做任何其他事情来获得发现的可能性。
,在DjangoAdmin网站的每个字段中编辑并添加值。不幸的是-查看和编辑工作按预期进行,但是当涉及DjangoAdmin的添加新用户时-我仍然只看到三个字段:用户名,密码和密码确认。
问题:当我由DjangoAdmin创建新用户时,应该在哪里编辑/编写一些代码来填充更多字段?
结尾:是-我已经尝试了很多google结果(包括使用 fieldsets, list_display, inline方法等。),尽可能多-我也想避免将OneToOne关系与User模型一起使用。那会产生很多错误,我放弃了,我不想回到那里。如果有这样的需要,我将提供代码段。
谢谢您的帮助。
我不确定问题是否出在NewUserAdmin类中的特定行顺序或其他内容-尚未测试过,但是我敢肯定我早就接近该解决方案了。但这确实是我在谈论的(它正在按照我想要的方式工作它到:))。现在,我可以轻松地将任何字段添加到用户模型,并且该字段仍连接到所有框架功能,例如会话管理,等等。而且,我不使用AbstractBaseUser或OneToOne字段,这引起了很多问题。无论如何-感谢您提供的任何帮助。没有您,我将无法解决...... ...本周:D
PS我跳过了所有进口商品。 IDE会告诉您,需要什么。
models.py
class NewUserManager(UserManager):
def create_user(self,username,email = None,password = None,** extra_fields):
返回UserManager.create_user(self,username,email = email,password =密码,** extra_fields)
def create_superuser(自己,用户名,电子邮件,密码,** extra_fields):
返回UserManager.create_superuser(自己,用户名,电子邮件,密码,** extra_fields )
类NewUser(AbstractUser):
newField = models.CharField()#例如CharField
newField = models.CharField(max_length = 12)#重要的事情-属性应该在第二行
对象= NewUserManager()
类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确认',widget = forms.PasswordInput)
类Meta:
模型= NewUser
字段=('username','email','first_name','last_name','newField')#一些示例字段和新字段:newField
def clean_password2(self):
password1 = self.cleaned_data.get( password1)
password2 = self.cleaned_data.get( password2)
如果password1和password2以及password1!= password2:
提出form.ValidationError(密码不匹配)
返回password2
def save(self,commit = True):
user = super(NewUserCreationForm,self).save(commit = False)
user.set_password(self.cleaned_data [ password1])
如果提交:
user.save()
返回用户
类NewUserChangeForm(UserChangeForm):
类Meta(UserChangeForm.Meta):
模型= NewUser
字段=(用户名,电子邮件,名字,姓氏, newField, last_login, 'is_staff')#几个示例字段和一个新字段:newField
admin.py
class NewUserAdmin(UserAdmin):
表格= NewUserChangeForm
add_form = NewUserCreationForm
list_display =( 'username','newField','first_name','last_name','last_login',)#几个示例字段和新字段:newField
fieldsets =(
(None,{'fields':( 'username','email','first_name','last_name','newField','last_login','is_staff')}),#个示例字段和新字段:newField
)
add_fieldsets =(
(无,{
'classes':('wide',),
'fields':('username','newField','password1', 'password2')}#这是您想在django-admin AddUser视图
中拥有的字段, #
之上的最重要的地方之一)
admin.site.register(NewUser,NewUserAdmin)
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.
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)
这篇关于django-admin中的新字段添加用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!