Задать вопрос

Тел: +7 965 3737 888

584

Просмотров

0

Ответов

Export queryset to Excel workbook

<h1>How to use</h1>
<p>Save the snippet to a file utils.py, and add the following view to your Django app:</p>
from django.http import HttpResponse
from .utils import queryset_to_workbook

def download_workbook(request):
    queryset = User.objects.all()
    columns = (
        'first_name',
        'last_name',
        'email',
        'is_staff',
        'groups')
    workbook = queryset_to_workbook(queryset, columns)
    response = HttpResponse(mimetype='application/vnd.ms-excel')
    response['Content-Disposition'] = 'attachment; filename="export.xls"'
    workbook.save(response)
    return response

<p>Note: you can use dotted notation ('foreign_key.foreign_key.field') in the columns parameter to access fields that are accessible through the objects returned by the queryset (in that case you probably want to use select_related with your queryset).</p>

Вопрос полезен? Да0/Нет0
file_2647.py(2.6Кб)
None