Save report to Django Excel with filter

Asked

Viewed 111 times

-1

Good afternoon...I made a function to export report in excel that worked, however comes everything that is in the database, I wanted it to be filtered...kindly someone could help me....

function that lists my model

# funcao para listas as alocacoes no template
@login_required
def listAlocacao(request):
    varios = request.GET.get('varios', None)
    context = {}
    if varios:
        alocacoes = Alocar.objects.filter(turma__curso__icontains=varios) | \
                    Alocar.objects.filter(turma__periodo__icontains=varios) | \
                    Alocar.objects.filter(turma__disciplina__icontains=varios) | \
                    Alocar.objects.filter(turma__professor__icontains=varios) | \
                    Alocar.objects.filter(dia__icontains=varios) | \
                    Alocar.objects.filter(horario__horario__icontains=varios) | \
                    Alocar.objects.filter(sala__sala__icontains=varios)
    else:
        alocacoes = Alocar.objects.all()

    context = {'alocacoes': alocacoes}
    return render(request, 'alocar/listalocacao.html', context)

function to export excel report

def export_users_xls(request):
    MDATA = datetime.now().strftime('%d-%m-%Y')
    response = HttpResponse(content_type='application/ms-excel')
    response['Content-Disposition'] = 'attachment; filename="alocacoes.xls"'

    wb = xlwt.Workbook(encoding='utf-8')
    ws = wb.add_sheet('Alocacoes')

    # Sheet header, first row
    row_num = 0

    font_style = xlwt.XFStyle()
    font_style.font.bold = True

    columns = ['BLOCO','SALA','CURSO','PERIODO','DISCIPLINA','PROFESSOR','DIA','HORARIO']

    for col_num in range(len(columns)):
        ws.write(row_num, col_num, columns[col_num], font_style)

    # Sheet body, remaining rows
    font_style = xlwt.XFStyle()

    rows = Alocar.objects.all().values_list('sala__bloco__bloco','sala__sala','turma__curso','turma__periodo','turma__disciplina','turma__professor','dia','horario__horario')

    for row in rows:
        row_num += 1
        for col_num in range(len(row)):
            ws.write(row_num, col_num, row[col_num], font_style)

    wb.save(response)
    return response

1 answer

0

You’re taking all the model records Alocar why are you using .all() in his Queryset in:

rows = Alocar.objects.all().values_list('sala__bloco__bloco','sala__sala','turma__curso','turma__periodo','turma__disciplina','turma__professor','dia','horario__horario')

If you want to filter by some attribute of Alocar you must replace the .all() for .filter(<ATRIBUTO_DE_ALOCAR>=<VALOR_DE_FILTRAGEM>)

You can read more about filter in the official documentation: https://docs.djangoproject.com/en/3.0/ref/models/querysets/#filter

  • @malves1982.... good morning...I understood what you are talking about in relation to (all()). my function filtering this in another view...where I have filtered queryset, however I can’t pull the filtered report from her...I even edited my post to show you...if you can help me thank you, this is a TCC job

  • Good morning @Elvisdba, from what I understand you call the view listAlocacao, listing allocations in some way and then calling the view export_users_xls to generate the report. Alocars are you interested. Recommend 1) pass the list of Ids from the first view to the second; or 2) pass this parameter varios from the first view to the second and repeat Queryset on the second to get the filtering you have in the first.

  • ...my difficulty lies in this point in question...make this communication.. because I am a little inexperienced with Jango...?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.