Freelancing for Pale Blue

Looking for flexible work opportunities that fit your schedule?


Effortless Django model import & export from the admin panel

Django Apr 26, 2024

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 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!

Tags

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.