Skip to main content

A simple django admin allow your export queryset to xlsx file.

Project description

django-simple-export-admin

A simple django admin allow your export queryset to xlsx file.

Install

pip install django-simple-export-admin

Usage

pro/settings.py

INSTALLED_APPS = [
    ...
    'django_static_fontawesome',
    'django_changelist_toolbar_admin',
    'django_simple_export_admin',
    ...
]
  • django_static_fontawesome, django_changelist_toolbar_admin and django_simple_export_admin are required.

app/models.py

from django.db import models

class Category(models.Model):
    name = models.CharField(max_length=32)

class Book(models.Model):
    name = models.CharField(max_length=32)
    author = models.CharField(max_length=32)
    category = models.ForeignKey(Category, on_delete=models.SET_NULL, null=True, blank=True)
    is_published = models.NullBooleanField()
    published_date = models.DateField(null=True)
    count = models.IntegerField(default=0)

    def preview(self):
        return "/preview/{0}.png".format(self.pk)
    preview.short_description = "Preview Image Path"

    class Meta:
        permissions = [
            ("export_filtered_books", "Export all filtered books"),
        ]

app/admin.py

from django.contrib import admin
from django_simple_export_admin.admin import DjangoSimpleExportAdmin
from django_simple_export_admin.admin import NullBooleanRender
from django_simple_export_admin.admin import DateRender
from django_simple_export_admin.admin import Sum
from .models import Book

class BookAdmin(DjangoSimpleExportAdmin, admin.ModelAdmin):
    list_display = ["name", "author"]
    list_filter = ["is_published", "published_date", "author"]

    django_simple_export_admin_exports = {
        "filtered-books": {
            "label": "Export All Filtered Books",
            "icon": "fas fa-book",
            "filename": "Book",
            "fields": [
                {"field": "forloop.counter1", "label": "Index"},
                {"field": "name", "label": "Book Name", "footer-value": "Sum:"},
                {"field": "count", "label": "Stock", "footer-value": lambda: Sum()},
                {"field": "category__name"},
                {"field": "author", "label": "Author", "empty_value": "-"},
                {"field": "is_published", "label": "Is Published", "render": NullBooleanRender("UNKNOWN", "YES", "NO")},
                {"field": "published_date", "label": "Published Date", "render": DateRender()},
            ],
            "export-filtered": True,
            "permissions": ["django_simple_export_admin_example.export_filtered_books"],
        }
    }
  • label default to _("Export").
  • icon default to None means no icon.
  • filename default to model_name.
  • export-filtered default to False, means always export all queryset without filtering.
  • permissions default to None, means only super admin have permission to do exporting.
  • fields
    • field can be field of the model instance, callable function of the model instance, callable function of the admin which takes model instance parameter. Similar with field name in list_display. field == forloop.counter1 will always display row index, e.g. 1,2,3...
    • label is the header of the column. If missing label, field.verbose_name or function.short_description will be used firstly. If still null, use the field name.
    • render is a callable object that transform the original value to display value.
    • empty_value only works when render is not provided, it is the display value for orignal None value.
    • footer-value is the value display at the bottom row. It can be an instance of Aggregate that accept every item value of this field and calc the final value at last. It can be a staic value.
    • start-row-index is row index where begin to write data. starts from 1.
    • xlsx-template template xlsx file path.
    • show-header True or False means show header or not.
    • col Which col the field value will be stored. Mostly it is 1-based index number. Use list so that you can split long string into multiple cells. Xlsx cell size limit to 32*1024 characters.

Shipped Renders

  • django_simple_export_admin.admin.ForceStringRender
  • django_simple_export_admin.admin.DateRender
  • django_simple_export_admin.admin.BooleanRender
  • django_simple_export_admin.admin.NullBooleanRender

Shipped Aggregates

  • django_simple_export_admin.admin.Sum
  • django_simple_export_admin.admin.Average
  • django_simple_export_admin.admin.Count

Releases

v0.1.0

  • First release.

v0.1.1

  • Fix footer-value problem. If use an instance of Aggregate in settings, the instance is used globally, so that the final value if not correct. So you must add lambda to dynamically to create a new instance very time.

v0.1.2

  • Fix document.

v0.1.3

  • Fix queryset limited by list_per_page problem, reset it to MAX_ROWS=999999.
  • Fix related field problem.
  • Change __row_index to forloop.counter1, because __ used for related object property.
  • If field label setting missing, found it's field-verbose-name or function-short-description first.
  • Use get_xxx_display() for choice field.
  • Add xlsx-template support.
  • Add start-row-index support.
  • Add show-header support.

v0.1.4

  • Fix loop.counter1 problem when start-row-index != 1.

v0.1.5

  • Fix requirements.txt in setup stage.
  • Add app_requires in django_simple_export_admin.init.py to work with django-app-requires.

v0.1.6

  • Add long string value split into multiple cells support.

v0.1.7

  • Doc update.

v0.1.9

  • Fix ugettext problem.

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-simple-export-admin-0.1.9.tar.gz (11.1 kB view details)

Uploaded Source

Built Distribution

django_simple_export_admin-0.1.9-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file django-simple-export-admin-0.1.9.tar.gz.

File metadata

File hashes

Hashes for django-simple-export-admin-0.1.9.tar.gz
Algorithm Hash digest
SHA256 fdb87d3a56b7c6b4344eae65d6848cc082879882e725a43a8a4df607f9d3ae9e
MD5 c800a4b480b4b88ac5e5b7c75c68b384
BLAKE2b-256 d537ce4ad3ec4e82d8c32f5afccf05fe5398684272d926e7b2b1843878ddb869

See more details on using hashes here.

File details

Details for the file django_simple_export_admin-0.1.9-py3-none-any.whl.

File metadata

File hashes

Hashes for django_simple_export_admin-0.1.9-py3-none-any.whl
Algorithm Hash digest
SHA256 bcbc7beea7afc5f83b2ac44cdaeb1c111c4184df6f0c23453c178db2c0d96877
MD5 27b757a2a114350addfa4e8be318ac91
BLAKE2b-256 7c1cc149fd62d01ec8d733d47746fbbeadd28c302bac913a7f5e8d481298929f

See more details on using hashes here.

Supported by

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