Search code examples
djangodjango-import-export

How to have only CSV, XLS, XLSX options in django-import-export?


I have implemented django-import-export for my project.

It provides me many file format options by default fro both import and export.

How to restrict the file formats to only CSV, XLS and XLSX ?

enter image description here


Solution

  • You can override the get_export_formats() method of the ExportMixin:

    from import_export.formats import base_formats
    
    
    class MyAdmin(ExportMixin):
        # your normal stuff
        def get_export_formats(self):
                """
                Returns available export formats.
                """
                formats = (
                      base_formats.CSV,
                      base_formats.XLS,
                      base_formats.XLSX,
                      base_formats.TSV,
                      base_formats.ODS,
                      base_formats.JSON,
                      base_formats.YAML,
                      base_formats.HTML,
                )
                return [f for f in formats if f().can_export()]