Admin Site

class xadmin.sites.AdminSite(name='xadmin')[source]

The core of xadmin, managing all the register contents of xadmin.

In general there is only one instance of AdminSite within a managed site, and this AdminSite instance is responsible for the following tasks:

  • register and manage all information required by xadmin
  • create admin view class
  • registered urls in django

xadmin required information includes:

  • the models need to be managed by xadmin and all information about the admin settings in the models
  • registered ‘admin view class’
  • registered ‘model admin view class’
  • all registered plugins
admin_view(view, cacheable=False)[source]

provide Decorator to all Views in the current AdminSite. Use has_permission method of AdminSite to check whether the current user has privilege to access the AdminSite, and redirect to login page if not.

it will be called inside the get_urls method of AdminSite

Parameters:cacheable – 默认情况下,所有的 AdminView 会通过 never_cache 标记成不做缓存,如果确实需要缓存,可以设置 cacheable=True

check the packages on that the xadmin depends whether are installed correctly.

In default, the ContentType module will be checked to see whether it is installed correctly


copy the information of the current AdminSite instance


use the get_view_class method of AdminSite to create AdminView class and return view method which can be called by the get_urls method

Parameters:admin_view_class – AdminView 类
create_model_admin_view(admin_view_class, model, option_class)[source]

use get_view_class method of AdminSite to create ModelAdminView and return view method, which can be call by get_urls method

  • admin_view_class – AdminView 类,该类应该为 ModelAdminView 的子类
  • model – Model 类,目前该参数暂无作用
  • option_class – Model 的 OptionClass,保存对该 Model 的相关定制
get_plugins(admin_view_class, *option_classes)[source]

the core method of xadmin is used to get the plugins from AdminViewClass

to obtain the plugin instances, first find the corresponding plugin classes registered to this AdminView class and all other integrated classes, then apply the OptionClass of the second parameter.

get_view_class(view_class, option_class=None, **opts)[source]

the most core method of xadmin is used to create specific AdminViewClass that belongs to xadmin

there are two steps of creating AdminView class instance and dynamically generating the mix class

  1. use the registered OptionClass (see method register), the option_class parameter and the view_class to dynamically create the class instance
  2. retrieve the corresponding plugins according to the view_class and its super classes, and create it as the ‘plugins’ properties of the AdminViewClass

if return True the request.user at least can access the current xadmin website, otherwise not.

register(model_or_iterable, admin_class=<type 'object'>, **options)[source]

register the Model that need to be managed, or register the OptionClass of the AdminView

  • model_or_iterable – 传入 model 或是指定的 ModelOptionClass
  • admin_class – 当 model_or_iterable 为 Model 时,该参数为 ModelAdmin;model_or_iterable 为 AdminView 时 ,该参数为 OptionClass

please see xadmin.views.base.BaseAdminPlugin about the details of Admin Plugin. For example:

from models import SomeModel

class SomeModelAdmin(object):

site.register(SomeModel, SomeModelAdmin)
register_modelview(path, admin_view_class, name)[source]

register the classes Model Base Admin View to the AdminSite

  • path – view对应的url路径
  • admin_view_class – 注册的 Model Base Admin View 类
  • name – view对应的url name, 要包含两个%%s, 分别会替换为 app_label和module_name

the registered Model Base Admin View in the AdminSite can be used to create AdminView for the registered Model which will contain the relevant Model information. Please check xadmin.views.base.ModelAdminView to see the details. For example:

from xadmin.views import ModelAdminView

class TestModelAdminView(ModelAdminView):
    def get(self, request, obj_id):

site.register_modelview(r'^(.+)/test/$', TestModelAdminView, name='%s_%s_test')

after the registration, the view can be accessed by path ‘/%(app_label)s/%(module_name)s/123/test’

register_plugin(plugin_class, admin_view_class)[source]

register the Plugin class to the instance of AdminSite, the instance of corresponding plugin class which is bound to the current view class will be effective when any instance of the AdminView classes runs

  • plugin_class – view对应的url路径
  • admin_view_class – 该 plugin 绑定的 Admin View 类

please see xadmin.views.base.BaseAdminPlugin about the details of Admin Plugin. For example:

from xadmin.views import BaseAdminPlugin

class TestAdminPlugin(BaseAdminPlugin):
    def get_context(self, context):
        context['test'] = True
        return context

site.register_plugin(TestAdminPlugin, SomeAdminView)

after the registration, the plugin will be called when the get_context method of SomeAdminView is invoked

register_view(path, admin_view_class, name)[source]

an independent admin page e.g. login page, introduction page, help page, ect can be created by registering the AdminView class to the AdminSite.

  • path – view对应的url路径
  • admin_view_class – 注册的 Admin View 类
  • name – view对应的url name

See xadmin.views.base.BaseAdminView to see the details about AdminView. For example:

from xadmin.views import BaseAdminView

class TestAdminView(BaseAdminView):
    def get(self, request):

site.register_view(r'test_view/$', TestModelAdminView, name='for_test')

after the registration, the users can access the view through the path of ‘/test_view/’


recover the information of the current AdminSite instance


unregister the Model or OptionClass

if the Model or OptionClass have never been registered, it throws exception of ‘xadmin.sites.NotRegistered’


return the urls attribute of the xdmin site, which is used to set django urls. This is property getter method and can be found in the ‘’ file of Django framework, the example shown as the following:

from django.conf.urls import patterns, include, url

import xadmin

urlpatterns = patterns('',
    url(r'', include(,
exception xadmin.sites.AlreadyRegistered[source]

if a model has been registered to AdminSite, an exception will be thrown when register it again.

class xadmin.sites.MergeAdminMetaclass[source]

it is used to generate the original class of admin view class

for now the original class has no special usage, but a new public properties will be added to it in the next version

exception xadmin.sites.NotRegistered[source]

if a model has not been registered to the AdminSite, an exception will be thrown when try to unregister this model.