All Projects → benkonrath → django-csv-export-view

benkonrath / django-csv-export-view

Licence: BSD-3-Clause License
Django class-based view for CSV exports

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to django-csv-export-view

csvlixir
A CSV reading/writing application for Elixir.
Stars: ✭ 32 (+88.24%)
Mutual labels:  csv, csv-export
Django Crud Ajax Login Register Fileupload
Django Crud, Django Crud Application, Django ajax CRUD,Django Boilerplate application, Django Register, Django Login,Django fileupload, CRUD, Bootstrap, AJAX, sample App
Stars: ✭ 118 (+594.12%)
Mutual labels:  csv, django-admin
org-clock-csv
Export Emacs org-mode clock entries to CSV format.
Stars: ✭ 80 (+370.59%)
Mutual labels:  csv
data-models
Collection of various biomedical data models in parseable formats.
Stars: ✭ 23 (+35.29%)
Mutual labels:  csv
shopify-product-csvs-and-images
Shopify product CSVs and images to seed your store with product data.
Stars: ✭ 76 (+347.06%)
Mutual labels:  csv
tableschema-go
A Go library for working with Table Schema.
Stars: ✭ 41 (+141.18%)
Mutual labels:  csv
flask-djcelery
An example project for configuring Djcelery with Flask application and dynamically changing tasks via REST API and through django admin
Stars: ✭ 13 (-23.53%)
Mutual labels:  django-admin
Jekyll
Call of Duty XAsset exporter that dumps raw assets from a game's memory.
Stars: ✭ 29 (+70.59%)
Mutual labels:  csv
csv-to-sqlite
A desktop app to convert CSV files to SQLite databases!
Stars: ✭ 68 (+300%)
Mutual labels:  csv
workbook
simple framework for containing spreadsheet like data
Stars: ✭ 13 (-23.53%)
Mutual labels:  csv
AndrOBD-Plugin
AndrOBD plugin development project
Stars: ✭ 38 (+123.53%)
Mutual labels:  csv
go-csv-tag
Read csv file from go using tags
Stars: ✭ 94 (+452.94%)
Mutual labels:  csv
datatools
A set of tools for working with JSON, CSV and Excel workbooks
Stars: ✭ 68 (+300%)
Mutual labels:  csv
csv2json
Writen in C, CSV file to JSON file/string converter with utf8 support.
Stars: ✭ 18 (+5.88%)
Mutual labels:  csv
AlphaVantageAPI
An Opinionated AlphaVantage API Wrapper in Python 3.9. Compatible with Pandas TA (pip install pandas_ta). Get your FREE API Key at https://www.alphavantage.co/support/
Stars: ✭ 77 (+352.94%)
Mutual labels:  csv
VBA-CSV-interface
The most powerful and comprehensive CSV/TSV/DSV data management library for VBA, providing parsing/writing capabilities compliant with RFC-4180 specifications and a complete set of tools for manipulating records and fields.
Stars: ✭ 24 (+41.18%)
Mutual labels:  csv
elm-csv
Decode CSV in the most boring way possible.
Stars: ✭ 23 (+35.29%)
Mutual labels:  csv
scala-csv-parser
CSV parser library.
Stars: ✭ 24 (+41.18%)
Mutual labels:  csv
flatpack
CSV/Tab Delimited and Fixed Length Parser and Writer
Stars: ✭ 55 (+223.53%)
Mutual labels:  csv
spparser
an async ETL tool written in Python.
Stars: ✭ 34 (+100%)
Mutual labels:  csv

django-csv-export-view

A Django class-based view for CSV export.

Build Status

Features

  • Easy CSV exports by setting a Django model and a fields or exclude iterable
  • Works with existing class-based view mixins for access control
  • Generates Microsoft Excel friendly CSV by default
  • Proper HTTP headers set for CSV
  • Easy to override defaults as needed
  • Easy integration into Django Admin

Installation

pip install django-csv-export-view

Quick Start

Examples:

from csv_export.views import CSVExportView

class DataExportView(CSVExportView):
    model = Data
    fields = ("field", "related", "property")

    # When using related fields you will likely want to override get_queryset() use select_related() or prefetch_related().
    def get_queryset(self):
        return super().get_queryset().select_related("related")
        OR
        return super().get_queryset().prefetch_related("related")

class DataExportView(CSVExportView):
    model = Data
    fields = ("field", "related__field", "property")

class DataExportView(CSVExportView):
    model = Data
    fields = "__all__"

class DataExportView(CSVExportView):
    model = Data
    exclude = ("id",)

    def get_queryset(self):
        queryset = super().get_queryset()
        return queryset.exclude(deleted=True)

class DataExportView(CSVExportView):
    model = Data

    def get_fields(self, queryset):
        fields = ["username", "email"]
        if self.request.user.is_superuser:
            fields.append("birth_date")
        return fields

fields / exclude: An iterable of field names and properties. You cannot set both fields and exclude. fields can also be "__all__" to export all fields. Model properties are not included when "__all__" is used. Related field can be used with __. Override get_fields(self, queryset) for custom behaviour not supported by the default logic.

model: The model to use for the CSV export queryset. Override get_queryset() if you need a custom queryset.

Further Customization

Examples:

from csv_export.views import CSVExportView

class DataExportView(CSVExportView):
    model = Data
    fields = "__all__"
    header = False
    specify_separator = False
    filename = "data-export.csv"

class DataExportView(CSVExportView):
    model = Data
    fields = "__all__"

    def get_filename(self, queryset):
        return "data-export-{!s}.csv".format(timezone.now())

header - boolean - Default: True
Whether to include the header in the CSV.

filename - string - Default: Dasherized version of verbose_name_plural from queryset.model.
Override get_filename(self, queryset) if a dynamic filename is required.

specify_separator - boolean - Default: True
Whether to include sep=<sepaator> as the first line of the CSV file. This is useful for generating Microsoft Excel friendly CSV.

CSV Writer Options

Example:

from csv_export.views import CSVExportView

class DataExportView(CSVExportView):
    model = Data
    fields = "__all__"

    def get_csv_writer_fmtparams(self):
        fmtparams = super().get_csv_writer_fmtparams()
        fmtparams["delimiter"] = "|"
        return fmtparams

Override get_csv_writer_fmtparams(self) and return a dictionary of csv write format parameters. Default format parameters are: dialect="excel" and quoting=csv.QUOTE_ALL. See all available options in the Python docs:

https://docs.python.org/3.9/library/csv.html#csv.writer

Django Admin Integration

Example:

from django.contrib import admin
from csv_export.views import CSVExportView

@admin.register(Data)
class DataAdmin(admin.ModelAdmin):
    actions = ("export_data_csv",)

    def export_data_csv(self, request, queryset):
        view = CSVExportView(queryset=queryset, fields="__all__")
        return view.get(request)

    export_data_csv.short_description = "Export CSV for selected Data records"

Contributions

Pull requests are happily accepted.

Alternatives

https://github.com/django-import-export/django-import-export/

https://github.com/mjumbewu/django-rest-framework-csv

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].