Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
938 views
in Technique[技术] by (71.8m points)

python - How to pass a filter from a dropdown into django-import-export view

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

My resource

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')
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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'.

Method 1

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)

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)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...