自定义admin组件
创建单例模式的几种方式:
1.__new__
2.模块的导入
·········································································································································································
admin源码分析:
urlpatterns = [
url(r'^admin/', admin.site.urls),
}
urls为一个元祖:
# url(r'^admin', (get_urls, None, None)),
单例模式:site=AdminSite() ---实例化的对象:
# 此处是部分源码
class AdminSite(object):
def __init__(self, name='admin'):
self._registry = {}
def register(self, model, admin_class=None, **options):
# 如果没有提供样式类,就用默认的ModelAdmin
if not admin_class:
admin_class = ModelAdmin
# 实例化admin类以保存在注册表中
self._registry[model] = admin_class(model, self)
# {Book:ModelAdmin(Book)}
样式类?
# 样式类
class BookConfig(admin.ModelAdmin):
list_display
list_display_links
search_fields
list_filter
action
admin.site.register(Book,BookConfig)
url分发:
分发: url(r"^admin/", ([ # 一级分发(确定app和model) url(r'^app01/book/', ([ # 二级分发(增删改查) url(r"^$", self.list_view), url(r"^add/$", self.add_view, name="add"), url(r"^(\d+)/change/$", self.change_view, name="change"), url(r"^(\d+)/delete/$", self.delete_view, name="delete") ], None, None)), url(r'^test02/', test02), url(r'^test03/', test03), ], None, None) )
自定义admin
1.admin 参数配置