Search code examples
django-import-export

How to change the exporting file's column names


The project I'm working on requires specific column names. Like the current export file has 'Surname' and 'First Name' but I need them to be 'lastname' and 'firstname', respectively.


Solution

  • Create a resource which is a subclass of ModelResource and override the get_export_headers() method:

    class BookResource(resources.ModelResource):
    
        def get_export_headers(self):
            headers = super().get_export_headers()
            for i, h in enumerate(headers):
                if h == 'Surname':
                    headers[i] = "lastname"
                if h == 'First Name':
                    headers[i] = "firstname"
            return headers
    
        class Meta:
            model = Book