介绍:stark组件,是一个帮助开发者快速实现数据库表的增删改查
目标:10s 完成一张表的增删改查
stark组件提供的接口说明:
list_display = [display_checkbox,]#自定义展示的列 /display_checkbox多选框
per_page_count = 10 #分页 展示每页数据10
has_add_btn = True #是否有添加按钮
model_form_class = None #应用模板表
order_list = [] #排序字段
search_list = [] #查询列表 里面加orm条件 search_list = ['name__contains', 'email']
action_list = [] # 批量操作 action_list = [StarkHandler.multi_delete, StarkHandler.multi_init] # 将函数传过去
search_group = [] #组合查询
change_list_template = None #类变量 展示界面的模板
add_template = None #类变量 展示界面的模板
change_template = None #类变量 展示界面的模板
delete_template = None #类变量 展示界面的模板
ps:可以根据业务需要重写v1.py StarkHandler类中的方法
def get_queryset(self,request,*args,**kwargs):
'''预留的钩子可过滤页面显示内容'''
return self.model_class.objects
def get_model_form_class(self,is_add,request,pk,*args,**kwargs):
'''
添加或编辑的ModelForm表单,可重写方法自定义modelform
:param is_add:是否是添加页面的表单,否则是编辑界面
:return:
'''
if self.model_form_class:
return self.model_form_class
else:
class DynamicModelForm(StarkModelForm):
class Meta:
model = self.model_class
fields = '__all__'
return DynamicModelForm
def save(self, request,form, is_update,*args,**kwargs):
#表单中没有显示的字段,设置到数据库 如 form.isntance.pid = 1
form.save()
def get_urls(self):
'''
默认是4个url,在继承的StarkHandler类中重写get_urls(),可以减少url、视图
:return:
'''
patterns = [
re_path(r'^list/$', self.wrapper(self.changelist_view), name=self.get_list_url_name),
re_path(r'^add/$', self.wrapper(self.add_view), name=self.get_add_url_name),
re_path(r'^change/(?P<pk>d+)/$', self.wrapper(self.change_view), name=self.get_change_url_name),
re_path(r'^delete/(?P<pk>d+)/$', self.wrapper(self.delete_view), name=self.get_delete_url_name),
]
patterns.extend(self.extra_url())
return patterns
def extra_url(self):
'''
预留的钩子,可添加视图
:return:
'''
return [] #re_path(r'xxx/$', self.wrapper(self.changelist_view), name=self.get_list_url_name),
示例
from stark.server.v1 import site,StarkHandler,StarkModelForm,StarkForm,Option
from stark.server.v1 import get_choice_text,get_datetime_text,get_m2m_text
from web import models
from django import forms
from django.urls import reverse
from django.utils.safestring import mark_safe
from stark.forms.DateTimePickerInput import DateTimePickerInput
from .base import PermissonHandler
class ClassListModelForm(StarkModelForm):
class Meta:
model = models.ClassList
fields = '__all__'
#自定义时间插件
widgets ={
'start_date':DateTimePickerInput,
'graduate_date':DateTimePickerInput,
}
class ClassListHandler(PermissonHandler,StarkHandler):
def display_course(self, obj=None, is_header=None):
'''自定义显示字段'''
if is_header:
return '班级'
return "%s%s期"%(obj.course.name,obj.semester) #Python全栈1期
def display_course_record(self, obj=None, is_header=None):
'''自定义显示字段'''
if is_header:
return '上课记录'
record_url = reverse('stark:web_courserecord_list', kwargs={'class_id': obj.pk})
return mark_safe('<a target="_blank" href="%s">上课记录</a>'% record_url)
list_display = ['school',display_course,'price',get_datetime_text('开班日期','start_date'),'class_teacher',get_m2m_text('任课老师','teach_teacher'),
display_course_record]
model_form_class = ClassListModelForm
search_group = [
Option('school'),
Option('course'),
]
site.register(models.ClassList,ClassListHandler)
源码示例下载:stark组件【stark.zip】