如何将下拉列表中的过滤器传递到 django-import-export 视图 [英] How to pass a filter from a dropdown into django-import-export view

查看:28
本文介绍了如何将下拉列表中的过滤器传递到 django-import-export 视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解如何通过具有 return render(request, 'htmlname.html, {}) 的视图传递过滤器.对于这种通过 django-import-export 导出选项导出数据的情况,我不知道该怎么做.我想从要下载的数据的下拉选择中传递过滤器.我的看法

I understand on how to pass a filter through views that have the return render(request, 'htmlname.html, {}). I don't know how to do it for this case of exporting data through django-import-export export option. I'd like to pass a filter from a dropdown selection for the data to be downloaded. My view

def ExportStudentsView(request):
    dataset = ExportStudentsResource().export(school = request.user.school,klass = ?????????)
    response = HttpResponse(dataset.xls, content_type='application/vnd.ms-excel')
    response['Content-Disposition'] = 'attachment; filename="All students.xls"'
    return response

我的资源

class ExportStudentsResource(resources.ModelResource):
    def export(self, queryset = None, *args, **kwargs):#this method helps capture the current user school
        queryset = Students.objects.filter(school = kwargs['school'],klass = kwargs['klass'])
        return super(ExportStudentsResource, self).export(queryset, *args, **kwargs)

    klass__name = fields.Field(attribute = 'klass__name',column_name='class')#Changes column name from school__name to school
    stream__name = fields.Field(attribute = 'stream__name',column_name='stream')
    gender__name = fields.Field(attribute = 'gender__name',column_name='gender')
    class Meta:
        model = Students
        fields = ('adm','name','kcpe','klass__name','stream__name','gender__name','notes')
        export_id_fields = ('adm',)
        export_order = ('adm','name','kcpe','klass__name','stream__name','gender__name','notes')

推荐答案

您可以采用几种方法.您选择的方法可能取决于这是否是一次性"的.或者如果你需要一个可重用的类.我可能更喜欢方法 2".

There are a couple of approaches you could take. The approach you choose might depend on whether this is a "one-off" or if you need a reusable class. I would probably favour 'method 2'.

您可以在视图中过滤查询集,然后将其传递给导出:

You could filter the queryset in your view, and then pass this to export:

def export_students(request):
  queryset = Students.objects.filter(school = kwargs['school'], klass = kwargs['klass'])
  # notice that the first arg to the Resource is a Queryset
  dataset = ExportStudentsResource().export(queryset)

方法二

覆盖资源上的 get_queryset() 方法.您可以在实例化时设置 args 并将它们应用于查询:

Method 2

Override the get_queryset() method on your resource. You can set args at instantiation and have them apply to the query:

class ExportStudentsResource(resources.ModelResource):

  def __init__(self, **kwargs):
    self.school = kwargs['school']
    self.klass = kwargs['klass']

  def get_queryset(self):
    return Students.objects.filter(school=self.school, klass=self.klass)

如果您查看 export() 它应该更清楚地说明发生了什么,以及它们如何组合在一起.

If you look at the source for export() it should make it clearer what is going on, and how this fits together.

这篇关于如何将下拉列表中的过滤器传递到 django-import-export 视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆