Django REST Framework CSV renderer with configurable field flattening
Project description
DRF Excel Renderer
A flexible Django REST Framework renderer for exporting data as CSV with advanced features including nested data flattening, streaming support, and configurable field handling.
Features
- Standard & Streaming CSV Export - Handle both small and large datasets efficiently
- Nested Data Flattening - Automatically flatten nested dictionaries and objects
- Configurable Field Handling - Preserve lists, customize separators, and control flattening behavior
- Easy Integration - Simple mixins for existing DRF views
- Memory Efficient - Streaming support for large datasets without memory issues
Installation
uv add drf-excel-renderer
Quick Start
Basic Usage
from drf_csv_renderer.views import CSVListView
from myapp.models import MyModel
from myapp.serializers import MyModelSerializer
class MyModelCSVView(CSVListView):
queryset = MyModel.objects.all()
serializer_class = MyModelSerializer
csv_filename = 'my_data.csv'
With Custom Configuration
class CustomCSVView(CSVListView):
queryset = MyModel.objects.all()
serializer_class = MyModelSerializer
# CSV Configuration
csv_filename = 'custom_export.csv'
csv_streaming = True # Enable streaming for large datasets
csv_flatten_nested = True # Flatten nested objects
csv_preserve_lists = False # Convert lists to comma-separated strings
csv_nested_separator = '.' # Use dots instead of underscores
csv_writer_options = {'delimiter': ';'} # Custom CSV writer options
Generic View for Custom Data
from drf_csv_renderer.views import CSVGenericView
class CustomDataCSVView(CSVGenericView):
csv_filename = 'custom_data.csv'
def get_csv_data(self):
# Return any data structure
return [
{'name': 'John', 'details': {'age': 30, 'city': 'NYC'}},
{'name': 'Jane', 'details': {'age': 25, 'city': 'LA'}}
]
Configuration Options
| Option | Default | Description |
|---|---|---|
csv_filename |
None |
Custom filename for CSV download |
csv_streaming |
False |
Enable streaming for large datasets |
csv_flatten_nested |
True |
Flatten nested dictionaries |
csv_preserve_lists |
True |
Keep lists as JSON vs comma-separated |
csv_nested_separator |
"__" |
Separator for nested field names |
csv_writer_options |
{} |
Additional options for CSV writer |
Data Flattening Examples
Nested Objects
# Input
{'user': {'name': 'John', 'profile': {'age': 30}}}
# Output (flattened)
{'user__name': 'John', 'user__profile__age': 30}
Lists Handling
# With preserve_lists=True (default)
{'tags': ['python', 'django']} → {'tags': ['python', 'django']}
# With preserve_lists=False
{'tags': ['python', 'django']} → {'tags': 'python, django'}
URL Configuration
from django.urls import path
from myapp.views import MyModelCSVView
urlpatterns = [
path('api/export/csv/', MyModelCSVView.as_view(), name='csv-export'),
]
Advanced Usage
Using Mixins Directly
from rest_framework import generics
from drf_csv_renderer.mixins import CSVResponseMixin
class MyCustomView(CSVResponseMixin, generics.ListAPIView):
queryset = MyModel.objects.all()
serializer_class = MyModelSerializer
csv_streaming = True
def get(self, request, *args, **kwargs):
if request.GET.get('format') == 'csv':
data = self.get_csv_data()
return self.create_csv_response(data)
return super().get(request, *args, **kwargs)
Custom Renderer
from drf_csv_renderer.renderers import CSVRenderer
class CustomCSVRenderer(CSVRenderer):
def _serialize_value(self, value):
# Custom serialization logic
if isinstance(value, datetime):
return value.strftime('%Y-%m-%d')
return super()._serialize_value(value)
Performance Considerations
- Use
csv_streaming = Truefor datasets larger than 1000 records - Streaming mode uses minimal memory but may be slower for small datasets
- Consider pagination for very large exports
- Nested flattening adds processing overhead
Requirements
- Python >= 3.12
- Django >= 5
- Django REST Framework >= 3.12
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
drf_csv_renderer-0.1.6.tar.gz
(11.9 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file drf_csv_renderer-0.1.6.tar.gz.
File metadata
- Download URL: drf_csv_renderer-0.1.6.tar.gz
- Upload date:
- Size: 11.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b59cb2152ed1be0d48f73ab6eebea74a1f52b3ed2aa4a9d02fc1dd42acd0b590
|
|
| MD5 |
e64c1ce3d527070297a812425cfdac4d
|
|
| BLAKE2b-256 |
21956710bd88079f90cb8d693ec4e53f5bed72ec41f73165364fde09588e67a9
|
File details
Details for the file drf_csv_renderer-0.1.6-py3-none-any.whl.
File metadata
- Download URL: drf_csv_renderer-0.1.6-py3-none-any.whl
- Upload date:
- Size: 8.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
562199b7f1b1ade7bf8403c4f490b262d5f29074f11b95e71b6002efcdcd0ecc
|
|
| MD5 |
16e0066f031e6f51ff2e5c85010f1854
|
|
| BLAKE2b-256 |
5cde6e95073aa2af35fb5e4b4e7f6275271bff4ba82f09ebcf700ae24a807499
|