本文介绍了使用自定义管理站点自动注册 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. urls.py 中导入 你的 admin.
  1. Create your own AdminSite with a simple __init__() override.
  2. Import your admin in urls.py.

更换 Django Admin 并获得 autodiscover() 行为是可能的.这是以典型的 django-admin startproject project 方式生成的项目结构:

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:(我认为在项目级别执行此操作最有意义.)

project/admin.py: (I think it makes the most sense to do this at the project level.)

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 的文件.导入当然会运行代码,并且该代码正在执行与注册模型相同的操作(例如 decorator方法).没有魔法.您不必向您的自定义管理员注册您的模型(如文档所述).

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 kwarg.这表明您可以通过自己的管理员调用 autodiscover() .不;那里没有接线(未来的功能?).分配发生在 here 并已修复到本地 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 身份验证模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-19 09:12