Skip to main content

Django Excel ViewSet

Project description

Django Excel ViewSet

Small improvement in the functionality of drf for exporting data into excel file.

Examples

# models.py
from django.db.models import Model


class Contact(Model):
    first_name = models.CharField( max_length=50)
    last_name = models.CharField(max_length=50)
    middle_name = models.CharField(max_length=50, blank=True)
    email = models.EmailField(blank=True, null=True)
    phone = models.CharField(max_length=15)

    class Meta:
        verbose_name = 'contact'
        verbose_name_plural = 'contacts'

# serializers.py
from rest_framework.serializers import ModelSerializer
from django_excel_viewset.serializers import ExcelSerializerMixin
from .models import Contact


class ContactExcelSerializer(ExcelSerializerMixin, ModelSerializer):
    # Settings excel table
    excel_filename = "users"
    excel_sheetname = "users"
    excel_table_label = "Users"
    datetime_cell_format = {"num_format": "dd.mm.yyyy h:mm;@", "border": 1, "border_color": "#000000"}
    date_cell_format = {"num_format": "dd.mm.yyyy", "border": 1, "border_color": "#000000"}
    percent_cell_format = {"num_format": "0.00"%"", "border": 1, "border_color": "#000000"}
    format_cell_border = {"border": 1, "border_color": "#000000", "text_wrap": True}
    format_header_table = {"bold": True, "align": "center", "border": 1, "border_color": "#000000", "text_wrap": True}
    table_label_format = {"italic": True, "font_size": 14}

    class Meta:
        model = Contact
        fields = (
            "id",
            "last_name",
            "first_name",
            "middle_name",
            "phone",
            "email",
        )
        extra_kwargs = {
            "last_name": {"help_text": "Last name"},
            "first_name": {"help_text": "First name"},
            "middle_name": {"help_text": "Middle name"},
            "phone": {"help_text": "Phone number"},
            "email": {"help_text": "Email"},
        }
# views.py
from django_excel_viewset.views import ExcelMixin
from rest_framework.viewsets import GenericViewSet
from .serializers import ContactExcelSerializer
from .models import Contact

class ContactViewSet(ExcelMixin, GenericViewSet):
    queryset = Contact.objects.all()
    serializer_class = ContactExcelSerializer

Environment variables

DJANGO_EXCEL_VIEWSET_LOCAL_TIMEZONE - setting time zone (default value "Europe/Moscow").

Required

  • python >=3.11, <4.0
  • django >=4.2, <6.0
  • djangorestframework >=3.11
  • pillow >=9.2
  • xlsxwriter >=1.3.6
  • pytz >=2020.1
  • environs >=9.3.1

Installation

pip install django-excel-viewset

Contribution

You can run tests with pytest.

pip install poetry
poetry install
pytest

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_viewset-0.0.1.tar.gz (11.6 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_viewset-0.0.1-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

Details for the file django_excel_viewset-0.0.1.tar.gz.

File metadata

  • Download URL: django_excel_viewset-0.0.1.tar.gz
  • Upload date:
  • Size: 11.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.3 Linux/6.11.0-1015-azure

File hashes

Hashes for django_excel_viewset-0.0.1.tar.gz
Algorithm Hash digest
SHA256 53bc651bbbf4111fd399a6c4098dfb327069737e5267657c3738957e51f30e9b
MD5 d4080154fc616c1d8015fe7bbd13f752
BLAKE2b-256 4629c2868e37d493b306b1ca3615c09c4443870801c61271573f8e75f68dde8d

See more details on using hashes here.

File details

Details for the file django_excel_viewset-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: django_excel_viewset-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 13.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.3 Linux/6.11.0-1015-azure

File hashes

Hashes for django_excel_viewset-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8171491b8be522b8fc0dcf038060087bf2a225acbd2e8c6a4ca81cd29fba3c3d
MD5 9caadd24fa2e02fa87e64bf073c55948
BLAKE2b-256 321a3b5d9efd7510730558c9aab595dff6fbe0e9a7666e995778ce60d2bef2b9

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