SimpleMiddleware使用方法求教

作者:rionking 533 浏览 1 评论 发布时间:2020-04-22 14:18:18

1、代码片段:

setting中使用:

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    # 加入simplepro的中间件
    # 'simplepro.middlewares.SimpleMiddleware',
]

模型:

class Department(models.Model):
    name = models.CharField(max_length=20, verbose_name='部门名称', help_text='部门名称必须唯一', blank=False,null=False ,db_index=True)
    subname = models.CharField(max_length=30, verbose_name='部门子名称', help_text='部门子名称对应部门名称',blank=True,null=True)
    is_enable = models.BooleanField(verbose_name='部门状态',help_text='部门是否激活',default=True)
    create_time = models.DateTimeField(verbose_name='创建时间', auto_now=True)

    class Meta:
        verbose_name = "部门"
        verbose_name_plural = "部门管理"
        unique_together = ('name', 'subname',)

    """
        如果有__unicode__ 方法,将会优先调用,没有在调用__str__方法
    """

    def __unicode__(self):
        if self.subname:
            return  '%s--%s' %(self.name,self.subname)
        else:
            return self.name

    def __str__(self):
        if self.subname:
            return  '%s--%s' %(self.name,self.subname)
        else:
            return self.name

View:

class DepartmentResource(resources.ModelResource):
      name = Field(attribute='name',column_name='部门名称')
      subname=Field(attribute='subname',column_name='子部门名称')

      class Meta:
            model =Department
            fields=()

@admin.register(Department)
class DepartmentAdmin(ImportExportMixin,admin.ModelAdmin):
      def get_queryset(self, request):
            qs = super(DepartmentAdmin,self).get_queryset(request)
            return qs

# #下面这段是为了调试跟踪list_filter值,没有其他作用,去掉对程序结果没有影响
# #---------------------------------------------------------------------
#             fltrs={}
#             if 'filters' in request.POST:
#                 print(request.POST['filters'])
#                 fltrs = json.loads(request.POST['filters'])
#             print('current filter str is {}'.format(fltrs))
# #---------------------------------------------------------------------

      resource_class = DepartmentResource
      list_display = ('id', 'name','subname', 'is_enable','create_time')
      list_filter = ('name',)
      list_editable=('subname','is_enable',)
      list_display_links=('name',)
      search_fields = ('name',)
      ordering = ('id',)
      list_per_page = 10
      actions_on_top = True

      actions = ['init']

      def init(self, request, queryset):

            init_depts = request.POST.get('ids').split(',')
            print(len(init_depts))
            #初始化数据 。。。
            return
      init.short_description = '计算当月工资'
      init.icon = 'fas fa-audio-description'
      init.type = 'danger'
      init.style = 'color:black;'

2、目前的效果:

3、期望的效果:

​ init_depts能包含所有选中的部门,但init_depts一直返回空的值



附加的:

​ 如果代码中在setting中加上simplepro中间件模块设置:

​ 'simplepro.middlewares.SimpleMiddleware',

效果如下图:

期望的效果:

​ 此时

​ init_depts能包含所有选中的部门,init_depts正确,但由于导出需要跳转到另外一个界面,导出时list_filter不起作用,当选中金融事业一部时,我只想导出金融事业一部的子部门,但结果会将整个部门表全部导出来。

求教:如何解?

Loading...
评论列表 1条评论
Jingle
2020-05-04 18:14:57

近期做import-export插件的兼容