用于创建AbstractUser扩展模型的Django管理表单

时间:2014-05-16 13:59:19

标签: python django forms admin

我有一个扩展/继承AbstractUser的用户模型。我还希望管理员中的用户创建表单匹配,但由于某种原因,我只能让它显示用户名和密码字段。没有其他的。

我觉得特别有趣的是,我在admin.py中对这3个字段所做的更改反映在创建表单中,但其他字段从不显示。例如,我可以更改密码1的帮助文本或标签,并在表单中呈现,但其他字段不会。

另外,如果我设置扩展UserAdmin并注册(如下面的代码所示),我会得到一个普通用户的3字段创建视图,但如果我扩展ModelAdmin,我会获得所有字段,但是可以'使用密码更新表格。它404s。

值得注意的是,进入对象列表的链接是用户'而不是' CommonUser'当我的模型被调用时,但这可能是某个地方的类元。


admin.py

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.forms import UserChangeForm, UserCreationForm
from models import CommonUser, Account, Registry
from django import forms


class MyUserChangeForm(UserChangeForm):
    class Meta(UserChangeForm.Meta):
        model = CommonUser


class MyUserCreationForm(UserCreationForm):

 password = forms.CharField(
    label='Password',
    max_length = 32,
    required=True,
    widget=forms.PasswordInput,
    )

password2 = forms.CharField(
    label='Confirm',
    max_length = 32,
    required=True,
    widget=forms.PasswordInput,
    help_text="Make sure they match!",
    )


class Meta(UserCreationForm.Meta):
    model = CommonUser
    fields = ['username', 'password', 'password2', 'email',
        'first_name','last_name','address','city','state','zipcode',
        'phone1','phone2',]
    help_texts = {
        'password': 'Must be at least 8 characters.',
    }


def clean_username(self):
    username = self.cleaned_data['username']
    try:
        CommonUser.objects.get(username=username)
    except CommonUser.DoesNotExist:
        return username
    raise forms.ValidationError(self.error_messages['duplicate_username'])


class MyUserAdmin(UserAdmin):
    form = MyUserChangeForm
    add_form = MyUserCreationForm
    fieldsets = UserAdmin.fieldsets + (
        ('Personal info', {'fields': ('address', 'phone1',)}),
    )

admin.site.register(CommonUser, MyUserAdmin)

(片段)model.py

from django.contrib.auth.models import AbstractUser

class CommonUser(AbstractUser):
    "User abstraction for carrying general info."

    WORK_STATES = (
            ('FL', 'FL'),
        )

    address = models.CharField(max_length=50)
    city = models.CharField(max_length=30)
    state = models.CharField(max_length=2, default='FL', choices=WORK_STATES)
    zipcode = models.CharField(max_length=10)
    phone1 = models.CharField(max_length=15)
    phone2 = models.CharField(max_length=15, null=True)
    gets_email_updates = models.BooleanField(default=False)

来源

Extending new user form, in the admin Django Using Django auth UserAdmin for a custom user model https://docs.djangoproject.com/en/1.6/topics/auth/customizing/#a-full-example

1 个答案:

答案 0 :(得分:7)

来自django.contrib.auth.admin的UserAdmin还设置了“add_fieldsets”属性,该属性设置要在添加用户视图中显示的字段。由于UserAdmin设置了此字段,因此您需要覆盖它以设置自己的字段。

以下是一个例子:

class CustomUserAdmin(UserAdmin):
# ...code here...

    fieldsets = (
        (None, {'fields': ('email',)}),
        (_('Personal info'), {'fields': ('first_name', 'last_name')}),
        (_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser',
                                       'groups', 'user_permissions')}),
        (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
    )
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('email', 'first_name', 'last_name', 'password1',
                       'password2')}
         ),
    )

希望这有帮助!

相关问题