stark组件使用流程 1.将stark组件拷贝到项目下,==》相当于一个app 2.注册stark的app, INSTALLED_APPS = [ 'stark.apps.StarkConfig',#必须写成该形式 ] 3.在要使用的app下创建srark.py文件#名字必须为stark 4.在项目的根urls.py下,添加urls from django.conf.urls import url from django.contrib import admin from stark.service.stark import site from api import views urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^asset/', views.Asset.as_view()), url(r'^stark/', site.urls),###### ] 5.简单的增删改查+模糊查询+批量操作 from stark.service.stark import site, StarkModelForm class BusinessUnitConfig(StarkConfig): list_display = [StarkConfig.display_checkbox, 'id', 'name'] search_list = ['name'] action_list = [StarkConfig.multi_delete, ] site.register(models.BusinessUnit, BusinessUnitConfig)#注册写的Config类 参数解释: StarkConfig.display_checkbox#批量操作使用的CheckBox多选框 list_display = [StarkConfig.display_checkbox, 'id', 'name']#要显示的字段 earch_list = ['name']#用于模糊查询 action_list = [StarkConfig.multi_delete, ]#用于批量操作 list_display中字段分为普通字段,choice字段,外键字段 普通字段:字段名即可 choice字段: 方式一: from stark.service.stark import get_choice_text get_choice_text('device_status_id', '状态') 方式二: 自定义show_device_status_id方法: def show_device_status_id(self, header=None, row=None): if header: return '变更记录' return row.get_device_status_id_display() 外键字段:直接写字段即可,但是对应的外键必须定义__str__方法 action_list中的方法可以自定义: def multi_pri(self, request,pk): '''公户变私户''' return render() redirect() HttpResponse() 6.简单的增删改查+自定义show方法+扩展URL+快速筛选+重写modelForm(校验规则)+DatePicker(日期选择)的运用 from stark.service.stark import site, Option, StarkModelForm from stark.forms.widgets import DatePickerInput class ServerForm(StarkModelForm): class Meta: model = models.Server fields = '__all__' widgets = { 'latest_date': DatePickerInput(attrs={'class': 'date-picker', 'autocomplete': 'off'}) } def clean(self): '''全局钩子''' 通过校验,返回self.clean,data 不通过校验,self.add_error('字段名','错误信息')或者raise ValueError class ServerConfig(StarkConfig): def show_hostname(self, header=None, row=None): if header: return '主机名' return mark_safe("<a href='/stark/repository/server/detail/{}/'>{}</a>".format(row.pk, row)) def server_assetrecord(self, request, pk): all_records = models.AssetRecord.objects.filter(server_id=pk).order_by('-create_at') return render(request, 'record.html', {'all_records': all_records}) def extra_url(self): '''额外的url''' urlpatterns = [ url(r'^assetrecord/(d+)/', self.server_assetrecord, name='server_assetrecord'), ] return urlpatterns list_display = [ show_hostname, ] list_filter = [ # 快速筛选 Option('business_unit', is_multi=True), Option('device_status_id', is_choice=True, text_func=lambda x: x[1], ) ] model_form_class = ServerForm site.register(models.Server, ServerConfig) Option参数介绍: # Option参数介绍 # field 外键 choice字段进行筛选 # condition={'id__lt':'3'} 显示id<3的业务线对象 # is_choice=True 是否为choice字段 # text_func=lambda x:x[1] 能够对传入的choice字段进行筛选显示 # value_func=lambda x:1 对值进行固定 # is_multi=True 对选,每个Option之间是OR关系,多个Option是AND的关系 model_form_class = ServerForm 7.简单的增删改查+自定义视图函数+无删除按钮 class IDNConfig(StarkConfig): #========================自定义视图=============================================== def changelist_view(self, request, *args, **kwargs): '''展示列表''' pass def change_view(self, request, pk, *args, **kwargs): '''编辑页面''' pass def delete_view(self, request, pk, *args, **kwargs): '''删除页面''' pass def add_view(self, request, *args, **kwargs): '''新增页面''' pass #==============================无删除按钮============================================ def get_urls(self): '''生成对应的url''' urlpatterns = [ url(r'^list/$', self.wrapper(self.changelist_view), name=self.get_list_url_name), url(r'^add/$', self.wrapper(self.add_view), name=self.get_add_url_name), url(r'^(?P<pk>d+)/change/', self.wrapper(self.change_view), name=self.get_change_url_name), # url(r'^(?P<pk>d+)/del/', self.wrapper(self.delete_view), name=self.get_del_url_name), ] extra = self.extra_url() if extra: urlpatterns.extend(extra) return urlpatterns def get_list_display(self): val = [] val.extend(self.list_display) val.append(StarkConfig.display_edit_del('edit')) return val 8.实现简单的增删改查 + prev 对一个model做两个增删改查(比如一张表生成公司胡页面) class BusinessUnitConfig(StarkConfig): list_display = ['id', 'name'] site.register(models.BusinessUnit, BusinessUnitConfig) class BusinessUnitConfig1(StarkConfig): list_display = ['id', 'name'] def get_queryset(self, request, *args, **kwargs): return self.model_class.objects.filter() site.register(models.BusinessUnit, BusinessUnitConfig1, 'v1')