All jobs

Add CSV Export to a Django REST Framework API

Yes. An agent adds a streamed CSV export endpoint beside your DRF list API, reusing its filters and auth, tests it with APIClient, and opens a PR.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The agent reads the list API it will mirror before it writes an export. In a project shaped like HackSoftware/Django-Styleguide-Example, requirements/base.txt pins Django==5.2.7, djangorestframework==3.16.1, django-filter==25.2 and psycopg2, and config/django/base.py sets DjangoFilterBackend as the default filter backend and an empty DEFAULT_AUTHENTICATION_CLASSES. styleguide_example/users/apis.py has UserListApi, an APIView with a nested FilterSerializer for id, is_admin and email, an OutputSerializer over BaseUser with id, email and is_admin, and a get() that validates the query, calls user_list(filters=...) from selectors.py, and returns get_paginated_response from styleguide_example/api/pagination.py. users/urls.py mounts it at /api/users/.

The export goes in the same file as UserExportApi, following the styleguide's rule that an API is one class with its own serializers. It reuses UserListApi.FilterSerializer so ?is_admin=true&email=... means the same thing on both routes, calls the same user_list selector, and returns a StreamingHttpResponse with text/csv, a Content-Disposition naming users.csv, and rows written by csv.writer through a generator over queryset.iterator(), so a large table never loads into memory. The columns are the OutputSerializer fields, written through that serializer so a later field change shows up in both JSON and CSV. urls.py gets path("export/", UserExportApi.as_view(), name="export"), and because the project's ApiAuthMixin in api/mixins.py is how authenticated APIs are declared, the export uses it, so only a logged-in user can download the table.

Tests follow authentication/tests/apis/test_user_login.py: a TestCase with APIClient, reverse("api:users:export"), and users created with user_create from users/services.py. The new users/tests/apis/test_user_export.py asserts that an anonymous request gets 401 or 403, that a logged-in request gets 200 with text/csv and the attachment header, that the body parses back with csv.reader into a header row plus one row per user with id, email and is_admin, and that ?is_admin=true returns only the admin rows. A type annotation on the generator keeps mypy --config mypy.ini happy under the django-stubs and djangorestframework-stubs plugins the project runs.

Before pushing, the agent runs what .github/workflows/django.yml runs on Python 3.12.4 with the Postgres service: ruff check ., mypy --config mypy.ini styleguide_example/ and pytest with DJANGO_SETTINGS_MODULE = config.django.test from pytest.ini. It also adds the route to the README's list API section, which already documents UserListApi.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "Add a CSV export to the users API in this Django REST Framework project and open a pull request. Read requirements/base.txt, config/django/base.py, config/django/test.py, styleguide_example/users/apis.py, styleguide_example/users/selectors.py, styleguide_example/users/filters.py, styleguide_example/users/urls.py, styleguide_example/api/mixins.py, styleguide_example/api/pagination.py, styleguide_example/authentication/tests/apis/test_user_login.py, pytest.ini, mypy.ini and .github/workflows/django.yml first. Add UserExportApi to users/apis.py using ApiAuthMixin, reusing UserListApi.FilterSerializer and the user_list selector, returning a StreamingHttpResponse with text/csv, a Content-Disposition attachment named users.csv, and rows from csv.writer over queryset.iterator() in the OutputSerializer's field order. Mount it at export/ in users/urls.py with the name export. Add users/tests/apis/test_user_export.py with APIClient: an anonymous request is refused, a logged-in request gets 200 with text/csv and the attachment header, the body parses with csv.reader into a header plus one row per user, and ?is_admin=true returns only admins. Run ruff check ., mypy --config mypy.ini styleguide_example/ and pytest, paste the output in the pull request, and document the route in README.md next to the list API. Do not change UserListApi, the selectors or the pagination.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
  1. Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
  2. Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
  3. Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

What comes back

What comes back is a pull request with UserExportApi in styleguide_example/users/apis.py, one line in users/urls.py, users/tests/apis/test_user_export.py with its __init__.py, and a short README section. The description shows the first lines of a CSV downloaded in the sandbox with curl against the running docker compose stack, and the output of ruff, mypy and pytest.

The repository's workflow runs the same three commands on the pull request, once in Docker and once against the Postgres service, so a mypy complaint from the stubs or a failing test shows as a red check rather than after merge.

A reviewer checks that the export is behind ApiAuthMixin, that it uses the same selector and filter serializer as the list so the two cannot drift, that the response streams rather than building a string, and that the column order matches the JSON output. Then they decide whether is_active belongs in the file, which the prompt leaves at the list's three fields.

Run it on every list API that a customer asks to download

A team that gets export requests per table sends one POST /o/{org}/sessions per repository with this prompt, naming the list API to mirror in input, and that repository in resources. Each run reads that API's filter and output serializers, so an orders list gets an orders export with its own columns, and each run opens its own pull request.

Give the API key a webhook_url and Rush POSTs run.completed when each turn ends, with the session_url to read the pull request from, so the support ticket that asked for the download can be answered with the link.

The use case this job follows, with its automation and trigger: Hand off a long task.

Why run it on Rush?

A CSV endpoint is a small feature with three ways to get it wrong: loading the whole table into memory, letting the export's filters drift from the list's, and forgetting the auth the list has. In a Rush sandbox the agent reads the styleguide's selector and serializer pattern, reuses them, streams with queryset.iterator(), and keeps every command in the run's record, so the pull request shows pytest and mypy passing, not a promise that they will.

The project runs ruff, mypy with the Django and DRF stub plugins, and pytest on every push, and a generator return type is the kind of detail that fails mypy on the first try. The agent runs all three before pushing, and the same request works unchanged on the next DRF project a team owns.

Frequently asked questions

Why not a DRF renderer class?

A CSVRenderer would make the export a format of the paginated list, so a client would get one page of rows per request. A separate APIView returning a StreamingHttpResponse writes the whole filtered table in one download and keeps the list API untouched.

Does it reuse the list's filters?

Yes. UserExportApi validates the query with UserListApi.FilterSerializer and calls the same user_list selector, so ?is_admin=true returns the same set in CSV as in JSON, and a new filter added to the serializer applies to both.

Who can download it?

Only an authenticated user, through the project's ApiAuthMixin, which accepts the session cookie, the Authorization: Session header and the JWT the project configures. The test asserts that an anonymous request is refused.

Will mypy pass?

The project runs mypy with django-stubs and djangorestframework-stubs, so the generator that yields rows needs an Iterator[str] annotation and the response needs the streaming type. The agent runs mypy --config mypy.ini styleguide_example/ in the sandbox and fixes what it reports before pushing.

More Django REST Framework jobs

The same job on other stacks

Keep reading