简体   繁体   English

django-admin中的新字段添加用户

[英]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. 我正在使用Django 1.7.1,在这里我遇到了不应该真正发生的问题。

Background: I'm working on customized user model based on AbstractUser. 背景:我正在研究基于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. 我设法通过UserChangeForm和Meta类使其与DjangoAdmin一起使用。

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. 问题:根据Django-docs,无需做任何其他事情就可以查看,编辑DjangoAdmin站点中的每个字段并为其添加值。 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". 不幸的是-查看和编辑工作按预期进行,但是当涉及DjangoAdmin的“添加新用户”时-我仍然只看到三个字段:“用户名”,“密码”和“密码确认”。

Question: Where should I edit/put some code to get possibility to fill more fields while I`m creating new user by DjangoAdmin? 问题:当我由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. 结束语:是的-我已经尝试了很多google结果(包括使用“ fieldsets”,“ list_display”,“ inline”方法等。),尽可能地,我-还要避免对用户模型使用OneToOne关系那产生了很多错误,我放弃了,我不想回到那里,如果有这样的需要,我将提供代码片段。

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. 我不确定问题是否出在NewUserAdmin类中的特定行顺序或其他问题上-尚未进行测试,但是我很确定自己早已接近此解决方案。 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. 而且-我不使用AbstractBaseUser或OneToOne字段,这引起了很多问题。 Anyway - thanks for any way of help. 无论如何-感谢您提供的任何帮助。 I wouldn't resolve it without you... ...this week :D 没有你,我不会解决... ...本周:D

PS I skipped all imports. PS我跳过了所有进口。 IDE will tell you, what is needed. IDE会告诉您,需要什么。

models.py 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM