Django-admin管理工具

一、Django admin组件使用

  Django提供了基于web的管理工具:amdin组件。

  admin可以对注册完的数据进行增删改成操作。

  一)项目的配置文件(settings.py)相关配置

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

# Application definition

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

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

  创建完django项目注意更改settings.py(注释最后一行)

        'DIRS': [os.path.join(BASE_DIR,  'templates')],

  二)激活管理工具(urls.py)

  通常我们在生成项目时,会在urs.py中自动设定好

from django.conf.urls import url
from django.contrib import admin

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

]

  三)使用管理工具

  1、创建管理用户,登录验证

  你可以通过命令 python manage.py createsuperuser 来创建超级用户。(也可创建普通用户)

  启动开发服务器,然后在浏览器中访问http://127.0.0.1:8000/admin/,得到登陆界面。

  

  2、数据库初始化文件models.py

 1 from django.db import models
 2 
 3 # Create your models here.
 4 
 5 
 6 class Author(models.Model):
 7 
 8     name=models.CharField( max_length=32)
 9     age=models.IntegerField()
10 
11 
12     def __str__(self):
13         return self.name
14 
15 class Publish(models.Model):
16 
17     name=models.CharField( max_length=32)
18     email=models.EmailField()
19 
20     def __str__(self):
21         return self.name
22 
23 
24 class Book(models.Model):
25 
26     title = models.CharField( max_length=32)
27     publishDate=models.DateField()
28     price=models.DecimalField(max_digits=5,decimal_places=2)
29 
30     publisher=models.ForeignKey(to="Publish")
31     authors=models.ManyToManyField(to='Author')
32 
33     def __str__(self):
34         return self.title
modes.py

  四)admin的定制

  1、使用默认配置

  在admin.py中只需要讲Mode中的某个类注册,即可在Admin中实现增删改查的功能

from .models import *
admin.site.register(models.UserInfo)

  2、注册model类到admin的两种方式

  若想进行更多的定制操作,需要利用ModelAdmin进行操作,如

  方式一:使用register的方法

class UserAdmin(admin.ModelAdmin):
        list_display = ('user', 'pwd',)
 
    admin.site.register(models.UserInfo, UserAdmin) # 第一个参数可以是列表

  方式二:使用register的装饰器

    @admin.register(models.UserInfo)                # 第一个参数可以是列表
    class UserAdmin(admin.ModelAdmin):
        list_display = ('user', 'pwd',)

  3、列表页选项

  页大小:每页显示多少条数据,默认为每页显示100条数据,修改django项目的admin.py文件

class AreaAdmin(admin.ModelAdmin):
    list_per_page = 100

  4、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、liest_filter,列表时,定制右侧快速刷选。(一对多,多对多字段)
@admin.register(Book)
class BookAdmin(admin.ModelAdmin):
    list_filter = ['publisher',]
    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(modles.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,详细页面时,使用filedsets标签对数据进行分割显示
@admin.register(models.UserInfo)
class UserAdmin(admin.ModelAdmin):
    fieldsets = (
        ('基本数据', {
            'fields': ('user', 'pwd', 'ctime',)
        }),
        ('其他', {
            'classes': ('collapse', 'wide', 'extrapretty'),  # 'collapse','wide', 'extrapretty'
            'fields': ('user', 'pwd'),
        }),
    )
    16、filter_vertical ,详细页面时,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(models.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)
admin.py

二、admin源码解析

  一)单例模式

  1、单例模式基本介绍

  单例模式(Singleton Pattern)是一种常用的软件设计模式,该模式的主要目的是确保某一个类只有一个实例存在。当你希望在整个系统中,某个类只能出现一个实例时,单例对象就能派上用场。

  比如,某个服务器程序的配置信息存放在一个文件中,客户端通过一个 AppConfig 的类来读取配置文件的信息。如果在程序运行期间,有很多地方都需要使用配置文件的内容,也就是说,很多地方都需要创建 AppConfig 对象的实例,这就导致系统中存在多个 AppConfig 的实例对象,而这样会严重浪费内存资源,尤其是在配置文件内容很多的情况下。事实上,类似 AppConfig 这样的类,我们希望在程序运行期间只存在一个实例对象。

  2、在 Python 中,我们可以用多种方法来实现单例模式:

  • 使用模块
  • 使用 __new__
  • 使用装饰器(decorator)
  • 使用元类(metaclass)
    1、使用__new__

  为了使类只能出现一个实例,我们可以使用__new__来控制实例的创建过程,代码如下

class Singleton(object):
    _instance = None
    def __new__(cls, *args, **kwargs):
        if not cls._instance:
            cls._instance = super(Singleton, cls).__new__(cls, *args, **kwargs)
        return cls._instance

class MyClass(Singleton):
    a = 1

one = MyClass()

two = MyClass()
one == two

print one is two                        #True
print ("id_one:",id(one),"id_two:",id(two))   #('id_one:', 78304144L, 'id_two:', 783041

  将类的实例和一个类的变量_instance关联起来,若cls._instance为None则创建实例,否则直接返回cls._instance

    2、使用模块

  Python模块就是天然的单例模式,因为模块在第一次导入时,会生成一个.pyc文件;当第二次导入时,就会直接加载.pyc文件,而不会再次执行模块代码。

  因此,我们只需将相关的函数和数据定义在一个模块中,就可以获得一个单例对象了。

#mysingleton.py
class MySingleton(object):
    def foo(self):
        pass

my_singleton = MySingleton()

  导入模块

from mysingleton import my_singleton
 
my_singleton.foo()

  二)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 site.py

class AdminSite(object):
    """
    An AdminSite object encapsulates an instance of the Django admin application, ready
    to be hooked in to your URLconf. Models are registered with the AdminSite using the
    register() method, and the get_urls() method can then be used to access Django view
    functions that present a full admin interface for the collection of registered
    models.
    """
......................................................
def register(self, model_or_iterable, admin_class=None, **options):
"""
Registers the given model(s) with the given admin class.

The model(s) should be Model classes, not instances.

If an admin class isn't given, it will use ModelAdmin (the default
admin options). If keyword arguments are given -- e.g., list_display --
they'll be applied as options to the admin class.

If a model is already registered, this will raise AlreadyRegistered.

If a model is abstract, this will raise ImproperlyConfigured.
"""
if not admin_class:
admin_class = ModelAdmin
......................................................
# This global object represents the default admin site, for the common case.
# You can instantiate AdminSite in your own code to create a custom admin site.
site = AdminSite()

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

  4、ModelAdmin(GeoModelAdmin) options.py

class GeoModelAdmin(ModelAdmin):
    """
    The administration options class for Geographic models. Map settings
    may be overloaded from their defaults to create custom maps.
    """

  5、执行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)   # 执行结果?

  6、admin的URL配置

  项目的urls.py文件

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

  7、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)),
]

 

posted @ 2018-03-19 14:23  思维无界限  阅读(268)  评论(0编辑  收藏  举报