CRM2Stark组件
CRM stark组件
- 开启新项目:CRM
- 再创建一个应用app02(python manage.py startapp app02 或者是工具栏:run...task:执行startapp app02)
- settings.py:INSTALLED_APPS配置:
- 'app01.apps.App01Config',
- 'app02.apps.App02Config',
-
看apps.py是否有自动生成对应的
class App01Config(AppConfig):
name = 'app01' -
2个应用的models.py:写入对应的表结构
- 表结构后面要加: def __str__(self): return self.表名的title/name/...字段 - 并执行: - >>>makemigrations - >>>migrate
-
注册:admin就是Django封装好的一个app而已:
- admin.py(app01)
- from .models import 表名
- admin.site.register(表名)
- admin.py(app02)
- from .models import 表名
- admin.site.register(表名)
-
启动应用,访问:127...../admin 要用户名和密码
-
createsuperuser
-
重启项目再次访问
- 查url:admin/app0.../表名
- 增url:admin/app0.../add
- 改url:admin/app0.../change
- 删url:admin/app0.../delete
以上就一行代码:增删改查所有都实现了,额外增加业务,只需要设计表结构就行了,我们的目标是自定义stark组件,学习源码:(既然是通过register()来实现的,我们先看register)
-
1启动每个app下的admin.py
-
2注册
-
def register(self,model_or_iterable,admin_class=None,**options):
-
admin_class = admin_class or ModelAdmin
-
[ModelAdim]:ModelAdim配置类
1, Django默认显示字段##
admin.py
class 表名Config(admin.ModelAdmin):--->继承
pass--->用了pass就代表默认用系统的
admin.site.register(表名,表名Config)--->默认用系统的
注意:不能重复register(表名)注册
2,自定义要显示的字段
admin.py
class 表名Config(admin.ModelAdmin):>>>>>继承ModelAdmin
list_display=['name','city','email']---->控制页面(字段)显示
admin.site.register(表名, 表名Config)>>>>>生成新的表格结构
3,显示关联表的字段(有多个,特殊)
admin.py
class BookConfig(admin.ModelAdmin):
def show_authors(self,obj):
return ",".join([obj.name for obj in obj.authors.all()])
list_display = ['title','pub_date','price',"show_authors"]
admin.site.register(Book,BookConfig)
4,自定义接口
class BookConfig(admin.ModelAdmin):
def del_action(self,obj):
return '<a href=''>删除</a>'
list_display = ["title","del_action"]
admin.site.register(Book,BookConfig)
Django的安全措施:a标签全部会显示,解决方案:
from django.utils.safestring import mark_safe
class BookConfig(admin.ModelAdmin):
def del_action(self,obj):
return mark_safe('<a href=''>删除</a>')
list_display = ["title","del_action"]
admin.site.register(Book,BookConfig)
admin定制
方式一:
class PublishConfig(admin.ModelAdmin):
list_display = ('name', 'city', 'email')
admin.site.register(Publish,PublishConfig) # 第一个参数可以是列表
方式二:
@admin.register(Publish) # 第一个参数可以是列表
class PublishConfig(admin.ModelAdmin):
list_display = ('name', 'email', 'city')
可定制功能
1. list_display=['','']按XX字段显示(1.0是放函数名.2.0是放函数名的字符串)
@admin.register(Book)
class BookConfig(admin.ModelAdmin):
list_display = ('title', 'price', 'authors')
def xxxxx(self, obj):
return "xxxxx"
2. list_display_links,列表时,定制列可以点击跳转。
@admin.register(Book)
class BookConfig(admin.ModelAdmin):
list_display = ('title', 'price', 'authors')
list_display_links = ('title',)
3. list_filter=['xx']过滤某个字段
- 一般不这么用,'10万本书'
- 一般按1对多的字段过滤list_filter=['publish']
- 多对多:list_filter==['publish','authors']
4. list_select_related,列表时,连表查询是否自动select_related
5. list_editable,列表时,可以编辑的列 直接可在该字段下面所有值进行修改编辑
@admin.register(Book)
class BookConfig(admin.ModelAdmin):
list_display = ('title', 'price', 'authors')
list_editable = ('title',)
6. search_fields,列表时,模糊搜索的功能
@admin.register(Book)
class BookConfig(admin.ModelAdmin):
search_fields = ('title', 'price')
7. date_hierarchy,列表时,对Date和DateTime类型进行搜索
@admin.register(Book)
class UserAdmin(admin.ModelAdmin):
date_hierarchy = 'pub_date'
8. action,列表时,定制action中的操作 short_description命名
class BookConfig(admin.ModelAdmin):
list_display = ["title", 'price', 'pub_date', "publish"]
def patch_init(self, request, queryset):
queryset.update(price=100)
patch_init.short_description = "批量初始化" # 命名
actions = [patch_init, ]
admin.site.register(Book, BookConfig)
9. 定制HTML页面
class BookConfig(admin.ModelAdmin):
list_display = ["title", 'price', 'pub_date', "publish"]
change_list_template = "mylist.html"
admin.site.register(Book,BookConfig)
10. 排序(降序-)
class BookConfig(admin.ModelAdmin):
list_display = ["title", 'price', 'pub_date', "publish"]
ordering = ["-price"]
admin.site.register(Book,BookConfig)
11. inlines,详细页面,如果有其他表和当前表做FK,那么详细页面可以进行动态增加和删除
12. 显示多对多:author和外键publish字段
class BookConfig(admin.ModelAdmin):
def show_authors(self,obj):
return ",".join([obj.name for obj in obj.authors.all()])
list_display = ['title','pub_date','price','publish',"show_authors"]
ordering = ['-price']
admin.site.register(Book,BookConfig)
路由一级分发
urls.py
path('index/',([
path('test01/',views.test01),
path('test02/',views.test02)],
None,
None)),---->None必须写
views.py
def test01(request):
return HttpResponse('test01')
def test02(request):
return HttpResponse('test02')
浏览器:...index/test01
路由二级分发
![](https: // img2018.cnblogs.com / blog / 1523703 / 201901 / 1523703 - 20190118145157917 - 1003858455.png)
二级分发应用
urls.py
from django.contrib import admin
from django.urls import path,re_path
from django.conf.urls import url
from django.shortcuts import HttpResponse
def add_view(request):
return HttpResponse("add_view...")
def list_view(request):
return HttpResponse("list_view...")
def change_view(request,id):
return HttpResponse("change_view...")
def delete_view(request,id):
return HttpResponse("delete_view...")
def get_urls2():
temp=[
path('', list_view),
path('add/', add_view),
re_path('(\d+)/change/', change_view),
re_path('(\d+)/delete/', delete_view),
]
return temp
def get_urls():
print("------>",admin.site._registry) # 键值对
#{Book:BookCongfigObj,Publish:PublishConfigObj,.....}
temp = []
for model,config_obj in admin.site._registry.items():
model_name=model._meta.model_name
app_label=model._meta.app_label
temp.append(
path('%s/%s/' % (app_label, model_name),
(get_urls2(),None,None)),
)
return temp
urlpatterns = [
path('yuan/',(get_urls(),None,None)),
]