Xadmin的实现

一、启动

1、前提

配置setting.py文件 INSTALLED_APPS

2、定位Xadmin,找到对应的类

'Xadmin.apps.XadminConfig',

3、根据admin源码,实现启动

from django.apps import AppConfig
from django.utils.module_loading import autodiscover_modules


class XadminConfig(AppConfig):
    name = 'Xadmin'

    # 启动Xadmin
    def ready(self):
        autodiscover_modules('Xadmin')

二、注册

1、根据需求,需要自定义 注册的对象和方法

根据admin源码

# 定义ModelXadmin, models的样式
class ModelXadmin:

    def __init__(self, model, site):
        self.model = model
        self.site = site


# 定义 XadminSite类
class XadminSite:

    def __init__(self, name='Xadmin'):
        self._registry = {}

    def register(self, model, admin_class=None, **options):
        if not admin_class:
            admin_class = ModelXadmin
        self._registry[model] = admin_class(model, self)


site = XadminSite()

2、注册

from Xadmin.service.Xadmin import site
from app01.models import Book, Publish, Author, AuthorDetail

# Register your models here.

site.register(Book)
site.register(Publish)
site.register(Author)
site.register(AuthorDetail)

三、设置url

1、设置url

from django.contrib import admin
from Xadmin.service.Xadmin import site
from django.urls import path
urlpatterns = [
    path('admin/', admin.site.urls),
    path('Xadmin/', site.urls),
]

2、分发url

from django.urls import path
from django.shortcuts import HttpResponse

# 定义ModelXadmin, models的样式
class ModelXadmin:

    def __init__(self, model, site):
        self.model = model
        self.site = site


# 定义 XadminSite类
class XadminSite:

    def list_view(self, request):
        print(request.path)
        return HttpResponse("list_view")

    def add_view(self, request):
        print(request.path)
        return HttpResponse("add_view")

    def change_view(self, request, id):
        print(request.path)
        return HttpResponse("change_view")

    def delete_view(self, request, id):
        print(request.path)
        return HttpResponse("delete_view")

    def __init__(self, name='Xadmin'):
        self._registry = {}

    def register(self, model, admin_class=None, **options):
        if not admin_class:
            admin_class = ModelXadmin
        self._registry[model] = admin_class(model, self)

    # 一级urls
    def get_urls(self):
        temp = []
        for models, admin_class_obj in self._registry.items():
            app_name = models._meta.app_label
            models_name = models._meta.model_name
            temp.append(path('%s/%s/' % (app_name, models_name), self.urls2)),
        return temp

    # 二级urls
    def get_urls2(self):
        temp = []
        temp.append(path('', self.list_view))
        temp.append(path('add/', self.add_view))
        temp.append(path('<id>/change/', self.change_view))
        temp.append(path('<id>/delete/', self.delete_view))
        return temp

    @property
    def urls(self):
        return self.get_urls(), None, None

    @property
    def urls2(self):
        return self.get_urls2(), None, None


site = XadminSite()

3、将有关样式的分发迁移到样式类中

from django.urls import path
from django.shortcuts import HttpResponse, render


# 定义ModelXadmin, models的样式
class ModelXadmin:

    def __init__(self, model, site):
        self.model = model
        self.site = site

    def list_view(self, request):
        print(request.path)     # /Xadmin/app02/food/
        print(self.model)   # <class 'app02.models.Food'>
        data_list = self.model.objects.all()
        # return HttpResponse("list_view")
        return render(request, "show_food.html", {"data_list": data_list})

    def add_view(self, request):
        print(request.path)
        return HttpResponse("add_view")

    def change_view(self, request, id):
        print(request.path)
        return HttpResponse("change_view")

    def delete_view(self, request, id):
        print(request.path)
        return HttpResponse("delete_view")

    # 二级urls
    def get_urls(self):
        temp = []
        temp.append(path('', self.list_view))
        temp.append(path('add/', self.add_view))
        temp.append(path('<id>/change/', self.change_view))
        temp.append(path('<id>/delete/', self.delete_view))
        return temp

    @property
    def urls(self):
        return self.get_urls(), None, None


# 定义 XadminSite类
class XadminSite:

    def __init__(self, name='Xadmin'):
        self._registry = {}

    def register(self, model, admin_class=None, **options):
        if not admin_class:
            admin_class = ModelXadmin
        self._registry[model] = admin_class(model, self)

    # 一级urls
    def get_urls(self):
        temp = []
        # admin_class_obj 是 ModelXadmin的对象
        for models, admin_class_obj in self._registry.items():
            app_name = models._meta.app_label
            models_name = models._meta.model_name
            temp.append(path('%s/%s/' % (app_name, models_name), admin_class_obj.urls)),
        return temp

    @property
    def urls(self):
        return self.get_urls(), None, None


site = XadminSite()

优点

1、每一个表对应不同的模板

2、在样式类中方便调用表的数据

self.models   # <class 'app02.models.Food'>         
models.Food

 

posted @ 2019-08-17 23:30  市丸银  阅读(173)  评论(0编辑  收藏  举报