本文介绍了Django(1.10)覆盖AdminSite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试用自己的自定义类覆盖AdminSite类。我遵循了django文档中的教程:,但是它不起作用。具体来说,我想用自己的类覆盖原始的AdminSite,而不仅仅是在项目中添加另一个管理站点。

I've tried to override AdminSite class with my own custom class. I followed tutorial from django's documentation: https://docs.djangoproject.com/en/1.10/ref/contrib/admin/#customizing-adminsite but it didn't work. To be specific, I'd like to override original AdminSite with my own class and not just add another admin site into my project.

我创建了自定义类MyAdminSite从类

I've created my custom class MyAdminSite which inherit from class

from django.contrib.admin import AdminSite


class MyAdminSite(AdminSite):
    pass

然后在我的应用urls.py中添加:

Then in my app urls.py I've add:

from django.conf.urls import url, include
import django.contrib.admin as admin
from .admin_site import MyAdminSite

admin.site = MyAdminSite()
admin.autodiscover()


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

似乎有效,但是管理员模型已注册到由MyAdminSite安装的AdminSite。

It seemed to work, but admin models are register to AdminSite insted of MyAdminSite.

我尝试了三种向自定义站点注册模型的方法:

I tried three ways of register models to my custom site:

@admin.register(Model)
class ModelAdmin(model.AdminModel):
...

通过这种方式将模型注册到原始AdminSite。

This way models are registered to original AdminSite.

第二种方式:

@admin.site.register(Model):
class ModelAdmin(model.AdminModel):
...

这不起作用并导致异常。

That don't work and cause exception. The ModelAdmin class isn't passed to register method.

最后一种方式:

class ModelAdmin(model.AdminModel):
...
admin.site.register(Model, ModelAdmin)

可以,但是在管理站点上,我只能看到我的模型,而不能看到Django admin(用户和组)的模型。

That works, but on admin site I can see only my models not models from Django admin (Users and Groups).

如何永久覆盖admin.site并将所有模型注册到MyAdminSite?

How can I permanently override admin.site and register all models to MyAdminSite?

推荐答案

我没有找到解决问题的方法

I didn't found the solution to my problem, but I have made a workaround.

首先,我们需要在应用中创建模块(例如admin.py),然后扩展AdminSite类:

First we need to create module in our app (e.g. admin.py) and then extend class AdminSite:

from django.contrib.admin import AdminSite

class MyAdminSite(AdminSite):
    ...

然后在模块底部,我们需要创建MyAdminSite实例并从Django注册内置模型:

Then on bottom of module we need to create instance of our MyAdminSite and register built-in models from Django:

site = MyAdminSite()
site.register(Group, GroupAdmin)
site.register(User, UserAdmin)

必要的导入:

from django.contrib.auth.models import Group, User
from django.contrib.auth.admin import GroupAdmin, UserAdmin

在我们的网站网址模块中,我们需要覆盖原始网站对象:

In our site url module we need to override original site object:

from .admin import site

admin.site = site
admin.autodiscover()
...
url(r'^admin/', admin.site.urls)
...

最后要做的就是注册我们的模型。我们需要记住的一件事是,我们不能像这样使用register作为装饰器:

Last change we need to do is register our models. One thing we need to remeber is that we can't use register as decorator like that:

@admin.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
    ...

或:

@admin.site.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
    ...

我们需要定义ModelAdmin类,然后在MyAdminSite对象上调用register:

We need to define our ModelAdmin class and then call register on our MyAdminSite object:

class MyModelAdmin(admin.ModelAdmin):
    ...
admin.site.register(MyModel, MyModelAdmin)

这是唯一对我有用的解决方案。

This is the only solution that is working for me.

这篇关于Django(1.10)覆盖AdminSite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 10:22