Search code examples
djangodjango-admindjango-import-export

Is there a way I can download the headers only as a file in Django ImportExportModelAdmin


Is there a way to just download a file that contains only the field titles?

enter image description here

For example, the download template button will download a file that has all the necessary fields as shown in the help text.

The downloaded file would look like this:

enter image description here

Any help is appreciated.


Solution

  • Here is one way. You need to declare a resource and override the export() method so that it creates a new Dataset containing only headers.

    class BookResource(ModelResource):
    
        class Meta:
            model = Book
    
        def export(self, queryset=None, *args, **kwargs):
            data = super().export(queryset, args, kwargs)
            return tablib.Dataset(headers=data.headers)