在Django中自定义管理表单,同时也使用自动发现 [英] Customizing an Admin form in Django while also using autodiscover

查看:147
本文介绍了在Django中自定义管理表单,同时也使用自动发现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想修改Django内置的 django.contrib.auth 模块的一些细节。具体来说,我想要一个不同的表单,使用户名成为一个电子邮件字段(并发送一个备用电子邮件地址)(我宁愿不要修改 auth 当您使用 autodiscover 与一起使用时,表单更改似乎需要。)

I want to modify a few tiny details of Django's built-in django.contrib.auth module. Specifically, I want a different form that makes username an email field (and email an alternate email address. (I'd rather not modify auth any more than necessary -- a simple form change seems to be all that's needed.)

$ ModelAdmin auth 我与 auth 自己的管理界面,并获得已经注册的错误。

When I use autodiscover with a customized ModelAdmin for auth I wind up conflicting with auth's own admin interface and get an "already registered" error.

看起来我必须创建自己的管理网站,枚举所有的模型,只有18课程,但它似乎是一个DRY问题 - 每个变化都需要添加到模型添加到定制的管理网站。

It looks like I have to create my own admin site, enumerating all of my Models. It's only 18 classes, but it seems like a DRY problem -- every change requires both adding to the Model and adding to the customized admin site.

或我应该用自己的版本 autodiscover 与排除一起编写,以便基本上导入所有管理员模块 auth

Or, should I write my own version of "autodiscover with exclusions" to essentially import all the admin modules except auth?

推荐答案

只需使用admin.site.unregister()。以下是我最近在管理员( nb is_active过滤现在在默认情况下在Django核心的用户模型中添加过滤用户),仍然在这里作为示例),所有DRY可以是:从django.contrib导入admin
从django.contrib.auth.admin导入UserAdmin
从django导入

None of the above. Just use admin.site.unregister(). Here's how I recently added filtering Users on is_active in the admin (n.b. is_active filtering is now on the User model by default in Django core; still works here as an example), all DRY as can be:

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User

class MyUserAdmin(UserAdmin):
    list_filter = UserAdmin.list_filter + ('is_active',)

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

这篇关于在Django中自定义管理表单,同时也使用自动发现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆