DEV Community

RockAndNull
RockAndNull

Posted on • Originally published at paleblueapps.com on

Effortless Django model import & export from the admin panel

Effortless Django model import & export from the admin panel

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

Enter fullscreen mode Exit fullscreen mode

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

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

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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!

Top comments (0)