使用自定义管理站点自动注册Django身份验证模型 [英] Auto register Django auth models using custom admin site

查看:95
本文介绍了使用自定义管理站点自动注册Django身份验证模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用默认管理站点的Django auth实现了身份验证管理,但随后我想使用自己的AdminSite重写某些行为:

I implemented authentication management using Django auth with the default admin site but then I wanted to use my own AdminSite to rewrite some behaviors:

class OptiAdmin(admin.AdminSite):
    site_title = "Optimizer site's admin"
    #...Other stuff here

然后注册我自己的模型:

Then registered my own models:

admin_site = OptiAdmin(name='opti_admin')
admin.site.register(MyModel, MyModelAdmin)
#Other stuff here

但是当我进入管理站点时,我只能看到我刚刚注册的模型,这对我来说听起来很公平,但是我希望在这个新的自定义站点中看到所有其他应用程序模型,包括身份验证的用户和组而且我不知道如何像默认管理员一样自动执行此操作,请帮助:)。

But when I go to the admin site I am only able to see the models I just registered, which sounds fair to me but I would like to see all the other apps models in this new custom site including the auth's users and groups and I don't know how to do this automatically like the default admin does, pls help :).

推荐答案


  1. 使用简单的 __ init __()覆盖创建自己的AdminSite。

  2. 导入 you r 管理员在 urls.py 中。

  1. Create your own AdminSite with a simple __init__() override.
  2. Import your admin in urls.py.

替换Django只需花费很少的精力,即可进行管理并获得 autodiscover()行为。以下是典型的 django-admin startproject项目方式生成的项目结构:

Replacing the Django Admin and getting the autodiscover() behavior is possible with minimal effort. Here's a project structure generated in the typical django-admin startproject project fashion:

project/
    manage.py
    project/
        __init__.py
        settings.py
        urls.py
        wsgi.py
        admin.py  # CREATE THIS FILE

project / admin.py:(I

from django.contrib.admin import *  # PART 1

class MyAdminSite(AdminSite):
    site_header = "My Site"

    def __init__(self, *args, **kwargs):
        super(MyAdminSite, self).__init__(*args, **kwargs)
        self._registry.update(site._registry)  # PART 2

site = MyAdminSite()

project / urls.py(摘要):

from . import admin  # PART 3

urlpatterns = [
    url(r'^admin/', admin.site.urls),
]

第1部分是简单的Python。通过将 django.contrib.admin 中的所有内容导入您的名称空间,它可以作为临时替代品。我想您不必这样做,但这有助于保持期望。第3部分,只需连接您的管理员即可。第2部分是真正的把戏。正如文档所述, autodiscover()被调用来完成工作。所有自动发现的工作都是通过 INSTALLED_APPS 尝试导入名为 admin.py 的文件。导入当然会运行代码,并且代码会执行与注册模型相同的操作(例如装饰器,并以方法)。没魔术您不必必须向自定义的管理员注册模型(如文档所述)。

Part 1 is simple Python. By importing everything from django.contrib.admin into your namespace, it acts as a drop-in replacement. I suppose you don't have to do this, but it helps preserve expectations. Part 3, simply connect up your admin. Part 2 is the real trick. As the documentation says, autodiscover() is called to do the work. All autodiscover does is go through INSTALLED_APPS attempting to import a file called admin.py. Importing runs the code of course and that code is doing the same thing you do to register models (example by decorator and example by method). No magic. You don't have to register your models with your customized admin (as the documentation says).

自动发现看上去比其智能得多 register_to 虚假。这表示您可以通过自己的管理员自己调用autodiscover()。不;没有电线连接(未来功能?)。分配发生在此处,并且已修复到本地AdminSite实例此处(或此处使用装饰器)。 Django contrib模型注册到该实例,所有第三方库也将注册。

Autodiscover looks smarter than it is with its register_to kwarg. That indicates you could call autodiscover() yourself passing your own admin. Nope; there's no wiring connected there (future feature?). The assignment happens here and is fixed to the native AdminSite instance here (or here using the decorator). Django contrib models register to that instance and so will any third-party libraries. It's not something you can hook into.

这里有个窍门, _registry 只是一个字典映射。让Django自动发现所有内容,然后复制映射。这就是 self._registry.update(site._registry)起作用的原因。 self是您自定义的AdminSite实例, site是Django的实例,您可以向其中一个注册模型。

Here's the trick though, _registry is just a dictionary mapping. Let Django autodiscover all the things and then just copy the mapping. That's why self._registry.update(site._registry) works. "self" is your customized AdminSite instance, "site" is Django's instance and you can register your models with either.

(最后一点:如果缺少模型,那是因为复制导入顺序。在复制 _registry 之前,所有向Django AdminSite的注册都需要进行。直接注册到自定义管理员可能是最简单的事情。)

(Final note: If models are missing, it's because of import order. All the registration to Django's AdminSite needs to happen before you copy _registry. Registering directly to your customized admin is probably the easiest thing.)

这篇关于使用自定义管理站点自动注册Django身份验证模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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