如何使用自定义AdminSite类? [英] How to use custom AdminSite class?

查看:162
本文介绍了如何使用自定义AdminSite类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪个是实现自己的 django.contrib.admin.sites.AdminSite 的最佳方法?

Which is the best way to implement my own django.contrib.admin.sites.AdminSite?

实际上,在 django.contrib.admin.autodiscover 中注册 INSTALLED_APPS 有问题。如果我在 urls.py 中使用我的自定义AdminSite类,则管理页面上没有显示任何应用程序。

Actually I get a problem with the registration of INSTALLED_APPS in django.contrib.admin.autodiscover. If I use my custom AdminSite class in urls.py, there were no apps displayed on the admin page.

我用一个小黑客修复了这个。我写了这个课程:

I fixed this with a litte hack. I wrote this class:

from django.contrib.admin.sites import site as default_site

class AdminSiteRegistryFix( object ):
    '''
    This fix links the '_registry' property to the orginal AdminSites
    '_registry' property. This is necessary, because of the character of
    the admins 'autodiscover' function. Otherwise the admin site will say,
    that you havn't permission to edit anything.
    '''

    def _registry_getter(self):
        return default_site._registry

    def _registry_setter(self,value):
        default_site._registry = value

    _registry = property(_registry_getter, _registry_setter)

实现我的自定义AdminSite这样:

And implement my custom AdminSite like this:

from wltrweb.hacks.django.admin import AdminSiteRegistryFix
from django.contrib.admin import AdminSite

class MyAdminSite( AdminSite, AdminSiteRegistryFix ):
    # do some magic
    pass        


site = MyAdminSite()

所以我可以使用这个网站 c $ c> urls.py 。

So I can use this site for urls.py.

任何人都知道更好的方法?因为我从一个下划线开始访问一个var,它只不过是一个黑客。我不喜欢黑客。

Anyone knows a better way? Since I access a var starting with a underscore it is no more than a hack. I don't like hacks.

编辑:另一种方法是重写 django.contrib.admin。自动发现功能,但在这种情况下,我会有冗余代码。

Another way would be to rewrite the django.contrib.admin.autodiscover function, but in this case I would have redundant code.

推荐答案

=https://docs.djangoproject.com/en/1.10/ref/contrib/admin/#customizing-the-adminsite-class =nofollow noreferrer> https://docs.djangoproject.com/en/ 1.10 / ref / contrib / admin /#customizing-the-adminsite-class

Quoting from https://docs.djangoproject.com/en/1.10/ref/contrib/admin/#customizing-the-adminsite-class


如果你想设置您自己的管理网站具有自定义行为,但是,您可以自由地对AdminSite进行子类化,并覆盖或添加任何您喜欢的内容。然后,只需创建一个AdminSite子类的实例(与实例化任何其他Python类的方式相同),并且注册模型和ModelAdmin子类,而不是使用默认的

我想这是最明确的方法,但也意味着您需要更改应用程序admin.py文件中的注册码。 p>

I guess that is the most explicit approach, but it also means that you need to change the register code in your apps admin.py files.


当您使用自己的AdminSite实例时,真的不需要使用自动发现,因为您可能会将所有的每个应用程序的admin.py模块导入你的myproject.admin模块。

There is really no need to use autodiscover when using your own AdminSite instance since you will likely be importing all the per-app admin.py modules in your myproject.admin module.

假设似乎是,一旦你开始编写你的自定义管理网站,它变得几乎项目具体的,你事先知道你想要包括哪些应用程序。

The assumption seems to be, that once you start writing your custom admin site, it becomes pretty much project specific and you know beforehand which apps you want to include.

所以如果你不想使用上面的hack,我只看到这两个选项。将所有注册呼叫替换为您的自定义管理站点,或者在您的管理站点模块中明确注册模型。

So if you don't want to work with the hack above, I only really see these two options. Replace all register calls to your custom admin site or register the models explicitly in your adminsite module.

这篇关于如何使用自定义AdminSite类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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