Package: django-csv-export-view
django-csv-export-viewA Django class-based view for CSV export.FeaturesEasy CSV exports by setting a Djangomodeland afieldsorexcludeiterableWorks with existing class-based view mixins for access controlGenerates Microsoft Excel friendly CSV by defaultProper HTTP headers set for CSVEasy to override defaults as neededEasy integration into Django AdminInstallationpip install django-csv-export-viewQuick StartExamples:fromcsv_export.viewsimportCSVExportViewfrom.modelsimportMyModelclassDataExportView(CSVExportView):model=MyModelfields=("field","related","property")# When using related fields you will likely want to override get_queryset() use select_related() or prefetch_related().defget_queryset(self):returnsuper().get_queryset().select_related("related")ORreturnsuper().get_queryset().prefetch_related("related")fromcsv_export.viewsimportCSVExportViewfrom.modelsimportMyModelclassDataExportView(CSVExportView):model=MyModelfields=("field","related__field","property")fromcsv_export.viewsimportCSVExportViewfrom.modelsimportMyModelclassDataExportView(CSVExportView):model=MyModelfields="__all__"fromcsv_export.viewsimportCSVExportViewfrom.modelsimportMyModelclassDataExportView(CSVExportView):model=MyModelexclude=("id",)defget_queryset(self):queryset=super().get_queryset()returnqueryset.exclude(deleted=True)fromcsv_export.viewsimportCSVExportViewfrom.modelsimportMyModelclassDataExportView(CSVExportView):model=MyModeldefget_fields(self,queryset):fields=["username","email"]ifself.request.user.is_superuser:fields.append("birth_date")returnfieldsfields/exclude: An iterable of field names and properties. You cannot set bothfieldsandexclude.fieldscan also be"__all__"to export all fields. Model properties are not included when"__all__"is used.
Related field can be used with__. Overrideget_fields(self, queryset)for custom behaviour not supported by the
default logic.model: The model to use for the CSV export queryset. Overrideget_queryset()if you need a custom queryset.Further CustomizationExamples:fromcsv_export.viewsimportCSVExportViewfrom.modelsimportMyModelclassDataExportView(CSVExportView):model=MyModelfields="__all__"header=Falsespecify_separator=Falsefilename="data-export.csv"fromcsv_export.viewsimportCSVExportViewfrom.modelsimportMyModelclassDataExportView(CSVExportView):model=MyModelfields="__all__"verbose_names=Falsefromdjango.utilsimporttimezonefromcsv_export.viewsimportCSVExportViewfrom.modelsimportMyModelclassDataExportView(CSVExportView):model=MyModelfields="__all__"defget_filename(self,queryset):return"data-export-{!s}.csv".format(timezone.now())header-boolean- Default:TrueWhether to include the header in the CSV.filename-string- Default: Dasherized version ofverbose_name_pluralfromqueryset.model.Overrideget_filename(self, queryset)if a dynamic filename is required.specify_separator-boolean- Default:TrueWhether to includesep=<sepaator>as the first line of the CSV file. This is useful for generating Microsoft
Excel friendly CSV.verbose_names-boolean- Default:TrueWhether to use capitalized verbose column names in the header of the CSV file. IfFalse, field names are used
instead.CSV Writer OptionsExample:fromcsv_export.viewsimportCSVExportViewfrom.modelsimportMyModelclassDataExportView(CSVExportView):model=MyModelfields="__all__"defget_csv_writer_fmtparams(self):fmtparams=super().get_csv_writer_fmtparams()fmtparams["delimiter"]="|"returnfmtparamsOverrideget_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.writerDjango Admin IntegrationExample:fromdjango.contribimportadminfromcsv_export.viewsimportCSVExportViewfrom.modelsimportMyModel@admin.register(MyModel)classDataAdmin(admin.ModelAdmin):actions=("export_data_csv",)defexport_data_csv(self,request,queryset):view=CSVExportView(queryset=queryset,fields="__all__")returnview.get(request)export_data_csv.short_description="Export CSV for selected Data records"ContributionsPull requests are happily accepted.Alternativeshttps://github.com/django-import-export/django-import-export/https://github.com/mjumbewu/django-rest-framework-csv