1

从下拉选择中传递过滤器后,我想转换为模型的 excel 内容。这是视图。

def ConvertToExcelView(request):
    response = HttpResponse(content_type='text/csv')
    writer = csv.writer(response)
    writer.writerow([('name'), ('adm'),('form'),('stream') ])
    for member in Marks.objects.filter(student__school__name=request.user.school).values_list('student__name',     'student__adm', 'student__klass__name', 'student__stream__name':
        writer.writerow(member)       
    response['Content-Disposition'] = 'attachment; filename="members.csv"'
    return response

有没有办法可以将其合并到视图中????

query = request.GET.get('view_classes')
    if query:
        queryset = (Q(student__klass__name__icontains = query))            
    return render(request,'students_marks.html',all_mark_results)

如果我对此不清楚,请要求更多说明。

4

1 回答 1

1

这相当简单。只需包含获取请求并为您打算从中获取选择的表单命名,即

def ConvertToExcelView(request):
    details = request.GET.get('s_details') #Assuming the form name is s_details
    queryset = (Q(student__klass__name__exact = details))
    response = HttpResponse(content_type='text/csv')
    writer = csv.writer(response)
    writer.writerow([('name'), ('adm'),('form'),('stream') ])
    for member in Marks.objects.filter(queryset).distinct().filter(student__school__name=request.user.school).values_list('student__name',     'student__adm', 'student__klass__name', 'student__stream__name':
        writer.writerow(member)       
    response['Content-Disposition'] = 'attachment; filename="members.csv"'
    return response

这将像魅力一样起作用。如果有您需要的解释,请回复。

于 2021-04-15T13:28:39.187 回答