Skip to main content

Django queryset Excel extraction

Project description

django-excel-extract

PyPI download month PyPI version Python versions Django Versions

django-excel-extract helps you easily export Django model data into an Excel file (.xlsx) with minimal setup.

Installation

pip install django-excel-extract

ORM

Avaliable ORM methods:

  • .all()
  • .get()
  • .filter()
  • .values()
  • .annotate()

Example

Follow the link to view main page: http://127.0.0.1:8000/app/index/

index

Extract Report Get button contains:

def extract_excel_get(request):
    queryset = Report.objects.get(id=1)

    exclude = ['id']

    excel = Excel(
        model=Report,
        queryset=queryset,
        file_name='report_get',
        title='Report',
        exclude=exclude,
        date_time_format='%d/%m/%Y',
    )

    return excel.to_excel()

Result:

index


Extract Report Filter button contains:

def extract_excel_filter(request):
    queryset = Report.objects.filter(priority=Priority.HIGH)

    exclude = ['id']

    excel = Excel(
        model=Report,
        queryset=queryset,
        file_name='report_filter',
        title='Report',
        exclude=exclude,
        date_time_format='%d/%m/%Y',
    )

    return excel.to_excel()

Result:

index


Extract Report Values button contains:

def extract_excel_values(request):
    queryset = Report.objects.annotate(
        days_passed=ExpressionWrapper(
            now() - F('created_at'),
            output_field=fields.DurationField(),
        )
    ).values(
        'id',
        'report_num',
        'status_report',
        'type_report',
        'priority',
        'days_passed',
    )

    aggregation_field_names = {'days_passed': 'Days Passed'}

    exclude = ['id']

    excel = Excel(
        model=Report,
        queryset=queryset,
        file_name='report_values',
        title='Report',
        exclude=exclude,
        date_time_format='%d/%m/%Y',
        annotation_fields_map=aggregation_field_names,
    )

    return excel.to_excel()

Result:

index


Features

  • Export any Django model QuerySet to Excel.
  • Flexible customize fields output (dates, datetimes, booleans, choices, annotated fields).
  • Exclude specific fields.
  • Supports ManyToMany and ForeignKey fields.
  • Supports main objects query (.all(), .get(), .values(), .annotate())
  • Simple integration into Django views.

Open source is love, and coffee is fuel. If my code helped you out, send a coffee my way ☕😉😎

https://buymeacoffee.com/dmitrytok

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_excel_extract-1.0.0.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_excel_extract-1.0.0-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file django_excel_extract-1.0.0.tar.gz.

File metadata

  • Download URL: django_excel_extract-1.0.0.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.10

File hashes

Hashes for django_excel_extract-1.0.0.tar.gz
Algorithm Hash digest
SHA256 66e181be52d62e3ed3e8f7976649518e820b00c37103001ff37721a1115ac1e2
MD5 c875252e7dd6696687ad17f5634437f6
BLAKE2b-256 5b856841756314194a9c41fa1ca5187407a211076b481f8dfccb1127a181afd6

See more details on using hashes here.

File details

Details for the file django_excel_extract-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_excel_extract-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 019588f6f4f18ae4812ff91a24ca9472eb35e53fa7e60baf2358be489e2136c2
MD5 047476fec918cb629bf87c3f5c4833c1
BLAKE2b-256 7fa63b9bcb98d90ccd0a373828aa31893a80cebe606c0a6854f309e80581754f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page