Effortless Django model import & export from the admin panel
Tags: django
Published: 2024-04-26 08:24:34

---
title: Effortless Django model import & export from the admin panel
published: true
date: 2024-04-26 08:22:48 UTC
tags: Django
canonical_url: https://www.paleblueapps.com/rockandnull/django-import-export-for-data-management/
---

 ![Effortless Django model import & export from the admin panel](https://images.unsplash.com/photo-1619302820124-e3b9d8a7f686?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=M3wxMTc3M3wwfDF8c2VhcmNofDF8fHRydWNrJTIwbG9hZGluZ3xlbnwwfHx8fDE3MTM1MTg4NjR8MA&ixlib=rb-4.0.3&q=80&w=2000)

Managing data in a Django application can get tedious, especially when you need to add or update a large number of records via the admin interface. The [`django-import-export`](https://django-import-export.readthedocs.io/?ref=paleblueapps.com) library comes to the rescue, offering a user-friendly way to import and export data directly from your Django admin panel.

In this post, we'll walk you through setting up `django-import-export` and explore a minor limitation to address.

## **Getting Started**

Let's install `django-import-export` using pip:

```
pip install django-import-export

```

Then add `import_export` to your `INSTALLED_APPS` list in `settings.py`:

```
INSTALLED_APPS = [
    # Your other apps...
    'import_export',
]

```

_settings.py_

Finally, in your app's `admin.py`, make your model admin class inherit from `ImportExportModelAdmin`:

```
from import_export.admin import ImportExportModelAdmin

class MyModelAdmin(ImportExportModelAdmin):
    # Your model admin definition...
    pass

```

_admin.py_

That's it! With these simple steps, you'll see import and export buttons appear in your Django admin panel for the respective model. These buttons allow you to import data from various formats like CSV, JSON, and Excel, and export data in similar formats.

## **A word on foreign keys**

There's a small caveat to consider. By default, `django-import-export` uses IDs when referencing foreign keys during import. This might not be ideal for readability.

To address this, we can leverage `ModelResource`. Here's a basic example:

```
class ReservationResource(resources.ModelResource):
    class Meta:
        model = Reservation

    def dehydrate_user(self, reservation):
        return reservation.user.email

    def before_import_row(self, row, **kwargs):
        user_email = row.get("user")
        if user_email:
            row["user"] =
                CustomUser.objects.filter(email=user_email).first().id
```

In this example, the `Reservation` model references a `CustomUser` that owns the reservation. When using the import-export mechanism, the user's ID will be visible, but this is not very readable. We prefer to show the email instead. Using the `dehydrate_user` and `before_import_row` we can export the user's email instead and then convert the email back to the user's ID on the reverse operation.

Finally, we need to link the `Resource` with the admin model:

```
class ReservationAdmin(ImportExportModelAdmin):
    # Your fields...
    resource_class = ReservationResource
```

_admin.py_

## **Conclusion**

`django-import-export` is a powerful tool for streamlining data management in your Django projects. With its straightforward setup and customization options for foreign keys, it can significantly improve your workflow. Remember to consult the official documentation for more advanced features and functionalities.

Happy coding!