django - 总结 - admin

由于admin在启动后会自动执行每一个app下的ready方法:
具体是由
from django.utils.module_loading import autodiscover_modules
这个函数完成的。
def autodiscover():
    autodiscover_modules('admin', register_to=site)
自动去全部app下 调用 admin
  • 建立一个组件:stark,
    • 在app.py里的配置编写ready方法:
      • 程序加载时,会去每一个app下找stark.py,并加载
  • 在每一个app下建立stark.py
    • 将表注册到stark中  

固然admin是写到了init里python

----------> 在setting里只写app/一直写到配置类   是有区别的,后者执行ready方法django

 
 
# models
# __all__ = ['tables1',]

# admin
# from . import models
# for tables in models.__all__:
#     admin.site.register(getattr(models,table))

  

 

1. 使用管理工具:app

经过命令 python manage.py createsuperuser 来建立超级用户。
ide

2. admin的定制:函数

方式一:
class UserAdmin(admin.ModelAdmin):
    list_display = ('user', 'pwd',)
admin.site.register(models.UserInfo, UserAdmin)

方式二:
@admin.register(models.UserInfo)
class UserAdmin(admin.ModelAdmin):
    list_display = ('user', 'pwd',)
list_display = ["name", "pwd", "date", "xxx"]  # 显示的字段,不能显示多对多字段
list_display_links = ["pwd"]  # 显示的字段哪些能够跳转
list_filter = ["pwd"]  # 哪些能够筛选
list_select_related = []  # 连表查询是否自动select_related
list_editable = ["name"]  # 能够编辑的列
search_fields = ["name"]  # 模糊搜索的字段
date_hierarchy = "date"  # 对Date和DateTime进行搜索

def add(self, request, queryset):
    pass

add.short_description = "在action框中显示的名称"

actions = [add, ]  # 定制action行为具体方法
actions_on_top = True  # action框显示在上?
actions_on_bottom = False  # action框显示在下?
actions_selection_counter = True  # 是否显示选择个数

  inlines,详细页面,若是有其余表和当前表作FK,那么详细页面能够进行动态增长和删除工具

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

10 定制HTML模板post

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框形式url

raw_id_fields = ('FK字段', 'M2M字段',)

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

fields = ('user',)

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

exclude = ('user',)

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

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显示时,数据移动选择(方向:上下和左右)

filter_vertical = ("m2m字段",) # 或filter_horizontal = ("m2m字段",)

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

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 = "指定列数据为空时,默认显示"

2、单例模式

1. 使用 __new__

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

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

class MyClass(Singleton):  
    a = 1

2. 使用模块

class My_Singleton(object):
    x = 12
    def foo(self):
        print(self.x)

my_singleton = My_Singleton()

print('ok')

将上面的代码保存在文件 mysingleton.py 中,而后这样使用:

from mysingleton import my_singleton
 
my_singleton.foo()

 


知识点:

url()的使用:
path('yuan/',([path('test01/',test01)],None,None)), # yuan/test01
        path('yuan/',([
                path('test01/',([
                    path('test04/',test04),                        # yuan/test01/test04
                    path('test05/',test05)                         # yuan/test01/test05
                                ],None,None)),
                path('test02/',test02),                            # yuan/test02
                path('test03/',test03)                             # yuan/test03
                            ],None,None))    
 
注: re_path(r'^test04/',test04), # 以test04开头;
re_path(r'test04/',test04), # 包含test04;
相关文章
相关标签/搜索