{"id":2932,"library":"djangorestframework-csv","title":"Django REST Framework CSV","description":"djangorestframework-csv provides tools to integrate CSV rendering and parsing into Django REST Framework APIs. It enables DRF serializers to output data as CSV and allows DRF parsers to consume CSV data. The current version is 3.0.2, with releases tending to be driven by Django and DRF compatibility updates rather than a fixed cadence.","status":"active","version":"3.0.2","language":"en","source_language":"en","source_url":"https://github.com/mjumbewu/django-rest-framework-csv","tags":["django","drf","csv","data-export","api"],"install":[{"cmd":"pip install djangorestframework-csv","lang":"bash","label":"Install Package"}],"dependencies":[{"reason":"Required for any Django project and DRF integration.","package":"Django"},{"reason":"Core dependency; this library extends DRF's functionality.","package":"djangorestframework"}],"imports":[{"symbol":"CSVRenderer","correct":"from rest_framework_csv.renderers import CSVRenderer"},{"note":"Use this for large datasets to avoid memory issues.","symbol":"CSVStreamingRenderer","correct":"from rest_framework_csv.renderers import CSVStreamingRenderer"},{"symbol":"CSVParser","correct":"from rest_framework_csv.parsers import CSVParser"}],"quickstart":{"code":"from rest_framework.response import Response\nfrom rest_framework.views import APIView\nfrom rest_framework_csv.renderers import CSVRenderer\n\nclass ProductListView(APIView):\n    renderer_classes = (CSVRenderer, )\n\n    def get(self, request, *args, **kwargs):\n        # In a real application, this would come from a database query\n        # or a DRF serializer output.\n        products = [\n            {'id': 1, 'name': 'Laptop', 'price': 1200.00},\n            {'id': 2, 'name': 'Mouse', 'price': 25.00},\n            {'id': 3, 'name': 'Keyboard', 'price': 75.00}\n        ]\n        return Response(products)\n\n# To integrate this, you would typically add it to a Django URLconf, e.g.:\n# from django.urls import path\n# urlpatterns = [\n#     path('products.csv', ProductListView.as_view(), name='product-csv'),\n# ]\n# When accessing /products.csv, the API will return CSV data.","lang":"python","description":"This example demonstrates how to integrate `CSVRenderer` into a Django REST Framework `APIView` to output a list of dictionary objects as CSV. When an endpoint using this renderer is accessed with a `.csv` format suffix (e.g., `/products.csv`), the response will be a CSV file. For very large datasets, consider using `CSVStreamingRenderer`."},"warnings":[{"fix":"Ensure your project is running on Python 3.6 or newer (as per general DRF recommendations).","message":"Python 2 support was completely removed in version 3.0.0. Projects using older Python versions must upgrade to Python 3.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Always check the `djangorestframework-csv` documentation or release notes for precise compatibility with your Django and DRF versions, especially before upgrading either framework.","message":"Ensure compatibility with your Django and Django REST Framework versions. Version 3.0.0 and above officially support Django 2.x, 3.x, 4.x and DRF 3.x. Older `djangorestframework-csv` versions might have narrower compatibility ranges.","severity":"gotcha","affected_versions":"<3.0.0"},{"fix":"For static headers: `class MyRenderer(CSVRenderer): header = ['id', 'name']`. For dynamic headers, override the `get_header` method or pass `renderer_context`.","message":"When rendering CSV, the default headers are automatically derived from dictionary keys or serializer fields. To specify custom headers or control field order, set the `header` attribute on your `CSVRenderer` subclass, or dynamically provide headers via `renderer_context={'header': ['field_a', 'field_b']}`.","severity":"gotcha","affected_versions":"All"},{"fix":"Replace `CSVRenderer` with `CSVStreamingRenderer` in your `renderer_classes` for endpoints returning potentially large amounts of data to enable streaming.","message":"For very large datasets, using `CSVRenderer` can consume significant memory as it loads all data into memory before rendering. Use `CSVStreamingRenderer` instead to stream data, which reduces memory footprint and improves performance for large exports.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}