How to Use django-adaptors

Django-adaptors is a valuable Django application that streamlines data import and export processes between various file formats and your Django models. It provides a flexible and efficient way to manage data exchange in your Django projects. This guide will explain how to install and use django-adaptors to import data from CSV files and export data to CSV files.

Installation

To get started, install django-adaptors using pip:

pip install django-adaptors

Next, add ‘django-adaptors’ to your INSTALLED_APPS list in your project’s settings.py file:

# settings.py

INSTALLED_APPS = [
# ...
'django_adaptors',
# ...
]

Importing Data from CSV

Let’s walk through an example of importing data from a CSV file into a Django model using django-adaptors.

1. Define Your Django Model

First, create your Django model. Here’s a simple example of a Person model:

# models.py

from django.db import models

class Person(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    email = models.EmailField(unique=True)

    def __str__(self):
        return f"{self.first_name} {self.last_name}"

2. Create a CSV Adapter

We’ll create a CSV adapter class to map the CSV file’s fields to the corresponding fields in your model. This adapter tells django-adaptors how to interpret the data in the CSV file.

# adapters.py

from django_adaptors import models
from .models import Person

class PersonCSVAdapter(models.CsvDbModel):
    class Meta:
        dbModel = Person
        delimiter = ';'  # Adjust this if your CSV uses a different delimiter
        has_header = True  # Set to False if your CSV file has no header row
        mapping = {
            'first_name': 'FirstName',
            'last_name': 'LastName',
            'email': 'Email',
        }
    

Here’s a breakdown of the code:

  • dbModel: Specifies the Django model to which data will be imported.
  • delimiter: The delimiter used in your CSV file (e.g., comma, semicolon).
  • has_header: Indicates if the CSV file has a header row.
  • mapping: A dictionary that maps model fields (keys) to CSV column headers (values).
See also  How to Reset ID Sequence in Django

Import Data

Now, you can import data from a CSV file into your model:

# import_data.py

from .adapters import PersonCSVAdapter

# Read CSV data from a file
with open('people.csv', 'r') as csv_file:
    content = csv_file.read()

# Import data using the adapter
result = PersonCSVAdapter.import_from_string(content)

# Check for errors
    if result.errors:
        print("Errors occurred during import:")
        for error in result.errors:
            print(error)
    else:
        print(f"Successfully imported {len(result.cleaned_objects)} records.")

Exporting Data to CSV

You can also export data from your Django models to a CSV file using django-adaptors.

Create an Export Adapter

Define a CSV adapter for exporting data:

# adapters.py (add to existing code)

    class PersonCSVExporter(models.CsvModel):
        first_name = models.CharField()
        last_name = models.CharField()
        email = models.EmailField()

        class Meta:
            delimiter = ';'
            has_header = True
    

Export Data

Use the exporter to write data to a CSV file:

# export_data.py

    from .adapters import PersonCSVExporter
    from .models import Person

    # Query the data you want to export
    people = Person.objects.all()

    # Convert queryset to CSV string
    csv_data = PersonCSVExporter.export_csv(people)

    # Write to a CSV file
    with open('exported_people.csv', 'w') as csv_file:
        csv_file.write(csv_data)
    

Handling Import Errors

django-adaptors provides mechanisms to handle errors during data import. When calling import_from_string(), it returns a result object containing:

  • errors: A list of errors encountered during import.
  • cleaned_objects: A list of successfully imported objects.
See also  How to add to manytomany field in Django

You can use this information to log errors or take corrective actions.

Importing Data from Other Formats

While this guide focuses on CSV files, django-adaptors also supports other data formats like XML. You can create adapters similar to the CSV adapter for different formats by extending the appropriate classes provided by django-adaptors.

Additional Resources

See also  How does Django connect to external database?