• Django admin


    Dajngo admin使用

    admin app

    Django 提供了基于 web 的管理工具。

    Django 自动管理工具是 django.contrib 的一部分。你可以在项目的 settings.py 中的 INSTALLED_APPS 看到它:

    settings.py中

    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        "app01"
    ]

    django.contrib是一套庞大的功能集,它是Django基本代码的组成部分。

    启用admin管理工具

    要启动admin管理工具,只需要在urls.py中启用admin对应的路由配置项即可。

    通常我们在使用PyCharm生成项目时会在 urls.py 中自动设置好。

    from django.conf.urls import url
    from django.contrib import admin
    
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
    
    ]

    使用admin管理工具

    首先先启动我们的Django项目,然后在浏览器中访问 http://127.0.0.1:8000/admin/,就能看到登陆界面。(具体网址和端口根据自己环境输入)

    使用 python manage.py createsuperuser 来创建管理员账号,来登录admin管理后台。

    想要在admin 界面管理某个数据模型(model),我们需要先将该数据模型(model)注册到 admin。

    admin.site.register(models.Publisher)
    admin.site.register(models.Book)
    admin.site.register(models.Author)

    在admin.py中只要按照上面的方式将某个模型类(model class)注册,即可在Admin中实现该model对应的数据表增删改查的功能。

    admin的定制

    这种方式的注册比较简单,如果想要进行更多的定制操作,需要利用ModelAdmin进行操作,如:

    # 注册方式1
    class PublisherAdmin(admin.ModelAdmin):
        list_display = ("name", "address")
    
    
    admin.site.register(models.Publisher, PublisherAdmin)
    
    
    # 注册方式2
    @admin.register(models.Book)
    class BookAdmin(admin.ModelAdmin):
        list_display = ("title", "price", "publish_date", "publisher")

    ModelAdmin支持的定制项

    ModelAdmin中提供了大量的可定制功能,如

     1. list_display,列表时,定制显示的列。

    @admin.register(models.UserInfo)
    class UserAdmin(admin.ModelAdmin):
        list_display = ('user', 'pwd', 'xxxxx')
     
        def xxxxx(self, obj):
            return "xxxxx"

    2. list_display_links,列表时,定制列可以点击跳转。

    @admin.register(models.UserInfo)
    class UserAdmin(admin.ModelAdmin):
        list_display = ('user', 'pwd', 'xxxxx')
        list_display_links = ('pwd',)

    3. list_filter,列表时,定制右侧快速筛选。

    4. list_select_related,列表时,连表查询是否自动select_related

    5. list_editable,列表时,可以编辑的列 

    @admin.register(models.UserInfo)
    class UserAdmin(admin.ModelAdmin):
        list_display = ('user', 'pwd','ug',)
        list_editable = ('ug',)

    6. search_fields,列表时,模糊搜索的功能

    @admin.register(models.UserInfo)
    class UserAdmin(admin.ModelAdmin):
         
        search_fields = ('user', 'pwd')

    7. date_hierarchy,列表时,对Date和DateTime类型进行搜索

    @admin.register(models.UserInfo)
    class UserAdmin(admin.ModelAdmin):
     
        date_hierarchy = 'ctime'

    8  inlines,详细页面,如果有其他表和当前表做FK,那么详细页面可以进行动态增加和删除

    class UserInfoInline(admin.StackedInline): # TabularInline
        extra = 0
        model = models.UserInfo
     
     
    class GroupAdminMode(admin.ModelAdmin):
        list_display = ('id', 'title',)
        inlines = [UserInfoInline, ]

    9 action,列表时,定制action中的操作

    @admin.register(models.UserInfo)
    class UserAdmin(admin.ModelAdmin):
     
        # 定制Action行为具体方法
        def func(self, request, queryset):
            print(self, request, queryset)
            print(request.POST.getlist('_selected_action'))
     
        func.short_description = "中文显示自定义Actions"
        actions = [func, ]
     
        # Action选项都是在页面上方显示
        actions_on_top = True
        # Action选项都是在页面下方显示
        actions_on_bottom = False
     
        # 是否显示选择个数
        actions_selection_counter = True

    安分的分割线


    10 定制HTML模板

    add_form_template = None
    change_form_template = None
    change_list_template = None
    delete_confirmation_template = None
    delete_selected_confirmation_template = None
    object_history_template = None

    11 raw_id_fields,详细页面,针对FK和M2M字段变成以Input框形式

    @admin.register(models.UserInfo)
    class UserAdmin(admin.ModelAdmin):
     
        raw_id_fields = ('FK字段', 'M2M字段',)

    12  fields,详细页面时,显示字段的字段

    @admin.register(models.UserInfo)
    class UserAdmin(admin.ModelAdmin):
        fields = ('user',)

    13 exclude,详细页面时,排除的字段

    @admin.register(models.UserInfo)
    class UserAdmin(admin.ModelAdmin):
        exclude = ('user',)

    14  readonly_fields,详细页面时,只读字段

    @admin.register(models.UserInfo)
    class UserAdmin(admin.ModelAdmin):
        readonly_fields = ('user',)

    15 fieldsets,详细页面时,使用fieldsets标签对数据进行分割显示

    @admin.register(models.UserInfo)
    class UserAdmin(admin.ModelAdmin):
        fieldsets = (
            ('基本数据', {
                'fields': ('user', 'pwd', 'ctime',)
            }),
            ('其他', {
                'classes': ('collapse', 'wide', 'extrapretty'),  # 'collapse','wide', 'extrapretty'
                'fields': ('user', 'pwd'),
            }),
        )

    16 详细页面时,M2M显示时,数据移动选择(方向:上下和左右)

    @admin.register(models.UserInfo)
    class UserAdmin(admin.ModelAdmin):
        filter_vertical = ("m2m字段",) # 或filter_horizontal = ("m2m字段",)

    17 ordering,列表时,数据排序规则

    @admin.register(models.UserInfo)
    class UserAdmin(admin.ModelAdmin):
        ordering = ('-id',)
        或
        def get_ordering(self, request):
            return ['-id', ]

    18. radio_fields,详细页面时,使用radio显示选项(FK默认使用select)

    radio_fields = {"ug": admin.VERTICAL} # 或admin.HORIZONTAL

    19 form = ModelForm,用于定制用户请求时候表单验证

    from app01 import models
    from django.forms import ModelForm
    from django.forms import fields
     
     
    class MyForm(ModelForm):
        others = fields.CharField()
     
        class Meta:
            model = models = models.UserInfo
            fields = "__all__"
     
    @admin.register(models.UserInfo)
    class UserAdmin(admin.ModelAdmin):
     
        form = MyForm

    20 empty_value_display = "列数据为空时,显示默认值"

     
    @admin.register(models.UserInfo)
    class UserAdmin(admin.ModelAdmin):
        empty_value_display = "列数据为空时,默认显示"
     
        list_display = ('user','pwd','up')
     
        def up(self,obj):
            return obj.user
        up.empty_value_display = "指定列数据为空时,默认显示"
    from django.contrib import admin
    
    # Register your models here.
    
    from .models import *
    
    
    
    
    class BookInline(admin.StackedInline): # TabularInline
        extra = 0
        model = Book
    
    class BookAdmin(admin.ModelAdmin):
    
        list_display = ("title",'publishDate', 'price',"foo","publisher")
        list_display_links = ('publishDate',"price")
        list_filter = ('price',)
        list_editable=("title","publisher")
        search_fields = ('title',)
        date_hierarchy = 'publishDate'
        preserve_filters=False
    
        def foo(self,obj):
    
            return obj.title+str(obj.price)
    
    
    
        # 定制Action行为具体方法
        def func(self, request, queryset):
            print(self, request, queryset)
            print(request.POST.getlist('_selected_action'))
    
        func.short_description = "中文显示自定义Actions"
        actions = [func, ]
        # Action选项都是在页面上方显示
        actions_on_top = True
        # Action选项都是在页面下方显示
        actions_on_bottom = False
    
        # 是否显示选择个数
        actions_selection_counter = True
    
    
    
        change_list_template="my_change_list_template.html"
    
    
    
    class PublishAdmin(admin.ModelAdmin):
         list_display = ('name', 'email',)
         inlines = [BookInline, ]
    
    
    
    admin.site.register(Book, BookAdmin) # 第一个参数可以是列表
    admin.site.register(Publish,PublishAdmin)
    admin.site.register(Author)
    class Person(object):
        def __init__(self, name, age):
            self.name = name
            self.age = age
    
    
    class Singleton(object):
        _instance = None
    
        def __new__(cls, *args, **kwargs):
            print(1)
            if not cls._instance:
                cls._instance = super(Singleton, cls).__new__(cls)
            return cls._instance
    
        def __init__(self, name, age):
            print(2)
            self.name = name
            self.age = age
    
    
    if __name__ == '__main__':
        p1 = Person("alex", 9000)
        p2 = Person("alex", 9000)
    
        print(p1 == p2)
        print(id(p1), id(p2))
    
        print("=" * 120)
    
        s1 = Singleton("alex", 9000)
        s2 = Singleton("alex", 9000)
        print(s1 == s2)
        print(id(s1), id(s2))

    使用模块方式

    其实,Python 的模块就是天然的单例模式,因为模块在第一次导入时,会生成 .pyc 文件,当第二次导入时,就会直接加载 .pyc 文件,而不会再次执行模块代码。因此,我们只需把相关的函数和数据定义在一个模块中,就可以获得一个单例对象了。

    如果我们真的想要一个单例类,可以考虑这样做:

    class Singleton(object):
        def __init__(self, name, age):
            self.name = name
            self.age = age
    
    
    p1 = Singleton("alex", 9000)

    使用下面的代码测试一下:

    from singleton import p1
    
    print(id(p1))
    print(p1.name)
    p1.name = "Bob"
    
    from singleton import p1
    
    print(id(p1))
    print(p1.name)

    剩下那两种自己查资料呗...

    admin执行流程

    <1>循环加载执行所有已经注册的app中的admin.py文件

    def autodiscover():
        autodiscover_modules('admin', register_to=site)

    <2>执行代码

    #admin.py
    
    class BookAdmin(admin.ModelAdmin):
        list_display = ("title",'publishDate', 'price')
    
    admin.site.register(Book, BookAdmin) 
    admin.site.register(Publish)

    <3> admin.site  

    这里应用的是一个单例模式,对于AdminSite类的一个单例模式,执行的每一个app中的每一个admin.site都是一个对象

    <4> 执行register方法

    admin.site.register(Book, BookAdmin) 
    admin.site.register(Publish)
    class ModelAdmin(BaseModelAdmin):pass
    
    def register(self, model_or_iterable, admin_class=None, **options):
        if not admin_class:
                admin_class = ModelAdmin
        # Instantiate the admin class to save in the registry
        self._registry[model] = admin_class(model, self)

    思考:在每一个app的admin .py中加上

    print(admin.site._registry)   # 执行结果?

    到这里,注册结束!

    <5> admin的URL配置

    urlpatterns = [
        url(r'^admin/', admin.site.urls),
    ]
    class AdminSite(object):
        
         def get_urls(self):
            from django.conf.urls import url, include
          
            urlpatterns = []
    
            # Add in each model's views, and create a list of valid URLS for the
            # app_index
            valid_app_labels = []
            for model, model_admin in self._registry.items():
                urlpatterns += [
                    url(r'^%s/%s/' % (model._meta.app_label, model._meta.model_name), include(model_admin.urls)),
                ]
                if model._meta.app_label not in valid_app_labels:
                    valid_app_labels.append(model._meta.app_label)
    
          
            return urlpatterns
    
        @property
        def urls(self):
            return self.get_urls(), 'admin', self.name

    <6>  url()方法的扩展应用

    from django.shortcuts import HttpResponse
    def test01(request):
        return HttpResponse("test01")
    
    def test02(request):
        return HttpResponse("test02")
    
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^yuan/', ([
                        url(r'^test01/', test01),
                        url(r'^test02/', test02),
    
                        ],None,None)),
    
    ]
    from django.conf.urls import url,include
    from django.contrib import admin
    
    from django.shortcuts import HttpResponse
    
    def change_list_view(request):
        return HttpResponse("change_list_view")
    def add_view(request):
        return HttpResponse("add_view")
    def delete_view(request):
        return HttpResponse("delete_view")
    def change_view(request):
        return HttpResponse("change_view")
    
    def get_urls():
    
        temp=[
            url(r"^$".format(app_name,model_name),change_list_view),
            url(r"^add/$".format(app_name,model_name),add_view),
            url(r"^d+/del/$".format(app_name,model_name),delete_view),
            url(r"^d+/change/$".format(app_name,model_name),change_view),
        ]
    
        return temp
    
    
    url_list=[]
    
    for model_class,obj in admin.site._registry.items():
    
        model_name=model_class._meta.model_name
        app_name=model_class._meta.app_label
    
        # temp=url(r"{0}/{1}/".format(app_name,model_name),(get_urls(),None,None))
        temp=url(r"{0}/{1}/".format(app_name,model_name),include(get_urls()))
        url_list.append(temp)
    
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^yuan/', (url_list,None,None)),
    ]
  • 相关阅读:
    使用 RestSharp 调用 WebAPI 接口
    Android Studio 下载安装目录
    多线程之await/async
    ScriptX进行Web打印
    Sqlserver 查询最新修改过的表、过程和视图等
    SqlServer中的bit类型
    .Net 6
    PDA 使用总结
    SQL Server 发布订阅 发布类型详解
    Profile对象
  • 原文地址:https://www.cnblogs.com/zyling/p/12831958.html
Copyright © 2020-2023  润新知