自定义UserAdmin模型

时间:2013-05-06 03:09:36

标签: django

我正在覆盖UsersAdmin的管理模型。我添加了一个新属性:gender这是数据库中的一列。为什么不工作?这就是我重写管理模型的方式

class UserAdmin(admin.ModelAdmin):
    list_display = ('email', 'first_name', 'last_name','gender')
    list_filter = ('is_staff', 'is_superuser')

    admin.site.unregister(User)
    admin.site.register(User, UserAdmin)

我收到一条错误消息:UserAdmin.list_display[3], 'gender' is not a callable or an attribute of 'UserAdmin' or found in the model 'User'

编辑:类UserAdmin来自库:django.contrib.auth.admin导入UserAdmin,这是类定义

class UserAdmin(admin.ModelAdmin):
    add_form_template = 'admin/auth/user/add_form.html'
    change_user_password_template = None
    fieldsets = (
        (None, {'fields': ('username', 'password')}),
        (_('Personal info'), {'fields': ('first_name', 'last_name', 'email',)}),
        (_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser',
                                       'groups', 'user_permissions')}),
        (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
    )
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('username', 'password1', 'password2')}
        ),
    )
    form = UserChangeForm
    add_form = UserCreationForm
    change_password_form = AdminPasswordChangeForm
    list_display = ('username', 'email', 'first_name', 'last_name', 'is_staff')
    list_filter = ('is_staff', 'is_superuser', 'is_active', 'groups')
    search_fields = ('username', 'first_name', 'last_name', 'email')
    ordering = ('username',)
    filter_horizontal = ('groups', 'user_permissions',)

    def get_fieldsets(self, request, obj=None):
        if not obj:
            return self.add_fieldsets
        return super(UserAdmin, self).get_fieldsets(request, obj)

    def get_form(self, request, obj=None, **kwargs):
        """
        Use special form during user creation
        """
        defaults = {}
        if obj is None:
            defaults.update({
                'form': self.add_form,
                'fields': admin.util.flatten_fieldsets(self.add_fieldsets),
            })
        defaults.update(kwargs)
        return super(UserAdmin, self).get_form(request, obj, **defaults)

    def get_urls(self):
        from django.conf.urls import patterns
        return patterns('',
            (r'^(\d+)/password/$',
             self.admin_site.admin_view(self.user_change_password))
        ) + super(UserAdmin, self).get_urls()

    def lookup_allowed(self, lookup, value):
        # See #20078: we don't want to allow any lookups involving passwords.
        if lookup.startswith('password'):
            return False
        return super(UserAdmin, self).lookup_allowed(lookup, value)

    @sensitive_post_parameters()
    @csrf_protect_m
    @transaction.commit_on_success
    def add_view(self, request, form_url='', extra_context=None):
        # It's an error for a user to have add permission but NOT change
        # permission for users. If we allowed such users to add users, they
        # could create superusers, which would mean they would essentially have
        # the permission to change users. To avoid the problem entirely, we
        # disallow users from adding users if they don't have change
        # permission.
        if not self.has_change_permission(request):
            if self.has_add_permission(request) and settings.DEBUG:
                # Raise Http404 in debug mode so that the user gets a helpful
                # error message.
                raise Http404(
                    'Your user does not have the "Change user" permission. In '
                    'order to add users, Django requires that your user '
                    'account have both the "Add user" and "Change user" '
                    'permissions set.')
            raise PermissionDenied
        if extra_context is None:
            extra_context = {}
        username_field = self.model._meta.get_field(self.model.USERNAME_FIELD)
        defaults = {
            'auto_populated_fields': (),
            'username_help_text': username_field.help_text,
        }
        extra_context.update(defaults)
        return super(UserAdmin, self).add_view(request, form_url,
                                               extra_context)

    @sensitive_post_parameters()
    def user_change_password(self, request, id, form_url=''):
        if not self.has_change_permission(request):
            raise PermissionDenied
        user = get_object_or_404(self.queryset(request), pk=id)
        if request.method == 'POST':
            form = self.change_password_form(user, request.POST)
            if form.is_valid():
                form.save()
                msg = ugettext('Password changed successfully.')
                messages.success(request, msg)
                return HttpResponseRedirect('..')
        else:
            form = self.change_password_form(user)

        fieldsets = [(None, {'fields': list(form.base_fields)})]
        adminForm = admin.helpers.AdminForm(form, fieldsets, {})

        context = {
            'title': _('Change password: %s') % escape(user.get_username()),
            'adminForm': adminForm,
            'form_url': form_url,
            'form': form,
            'is_popup': '_popup' in request.REQUEST,
            'add': True,
            'change': False,
            'has_delete_permission': False,
            'has_change_permission': True,
            'has_absolute_url': False,
            'opts': self.model._meta,
            'original': user,
            'save_as': False,
            'show_save': True,
        }
        return TemplateResponse(request,
            self.change_user_password_template or
            'admin/auth/user/change_password.html',
            context, current_app=self.admin_site.name)

    def response_add(self, request, obj, post_url_continue=None):
        """
        Determines the HttpResponse for the add_view stage. It mostly defers to
        its superclass implementation but is customized because the User model
        has a slightly different workflow.
        """
        # We should allow further modification of the user just added i.e. the
        # 'Save' button should behave like the 'Save and continue editing'
        # button except in two scenarios:
        # * The user has pressed the 'Save and add another' button
        # * We are adding a user in a popup
        if '_addanother' not in request.POST and '_popup' not in request.POST:
            request.POST['_continue'] = 1
        return super(UserAdmin, self).response_add(request, obj,
                                                   post_url_continue)

    admin.site.register(Group, GroupAdmin)
    admin.site.register(User, UserAdmin)


class User(AbstractUser):
"""
Users within the Django authentication system are represented by this
model.

Username, password and email are required. Other fields are optional.
"""
gender = models.CharField(_('gender'),max_length=30)


class Meta:
    swappable = 'AUTH_USER_MODEL'

2 个答案:

答案 0 :(得分:0)

阅读"Extending the User model"页面的Customising Authentication in Django部分。它提供了一个示例,说明如何使用一对一关系在自定义User类中包含其他信息,以及如何将这些新字段添加到Django管理员的用户页面。

答案 1 :(得分:-1)

首先我认为发生的事情是可能会发生名称冲突。

根据您发布的内容,您取消注册Django User模型,然后重新注册Django User模型。 没有看到你的import语句,这只是一个疯狂的猜测。

我会将我的Django User模型的实现重命名为CustomUser,这就是说,如果您注册的内容实际上是您的拥有用户模型然后你缺少属性。

只是将一些代码删除会导致我猜测你模拟当前的代码

class User(AbstractBaseUser):
    identifier = models.CharField(max_length=40, unique=True, db_index=True)
    email = models.EmailField(max_length=75)
    USERNAME_FIELD = 'identifier'

    #and the rest of your custom attributes.

    def is_active(self):
        return "your custom implementation"

    def get_full_name(self):
        return "your custom implementation"

    def get_short_name(self):
        return "your custom implementation"

    def is_staff(self):
        return "your custom implementation"

    def is_superuser(self):
        return "your custom implementation"

因此,您的模型还有其他字段,例如email,在此下方您应添加gender = models.CharField(max_length=1, unique=True)