Skip to content

Import and export

Export

The list page has an Export menu next to Create. It downloads the list as it is on screen: the same search, filters and sort, every page of it, as CSV, Excel (XLSX) or JSON.

from hyperadmin.core.model import ModelAdmin
from hyperadmin.core.options import AdminOptions


class InvoiceAdmin(ModelAdmin):
    options = AdminOptions(
        list_display=["number", "customer", "status", "total"],
        search_fields=["number", "customer"],
        list_filter=["status"],
        # Optional: the columns of the file, when they differ from the list's.
        export_fields=["number", "customer", "status", "total", "issued_at"],
        export_formats=["csv", "xlsx", "json"],  # the default; [] turns export off
        export_max_rows=50_000,  # default 100000
    )

Who can export

Exporting needs two permissions: view_<model> and export_<model>. The admin creates export_<model> for every registered model at startup, like view_ and change_, but grants it to no one: until you give it to a group or a user (for example in the permission matrix, where it is the Export column), only superusers can export. A user without it sees no Export menu, and the export URL answers 403.

With no auth_backend the admin is open, and everyone may export.

A model with a composite primary key is registered list-only and has no Export menu.

What the file holds

  • Rows. The rows the list shows on all its pages: the same get_queryset scoping (row-level security), search, filters and sort. With an object_permission_checker, each row is checked for view and rows that fail are left out.
  • Columns. export_fields, or the list's columns (list_display) when it is None. Dotted paths such as "customer.name" work as in list_display. Sensitive fields (password hashes, tokens, anything sensitive_fields marks) are never written, even when export_fields names them.
  • Order. The list's sort, with the primary key breaking ties.
Format Content-Type Notes
CSV text/csv; charset=utf-8 Header row first, \r\n line ends, UTF-8 with a byte-order mark
XLSX application/vnd.openxmlformats-officedocument.spreadsheetml.sheet One sheet named after the model; numbers, booleans and dates stay typed. Needs openpyxl
JSON application/json One array of objects keyed by column; dates in ISO 8601, decimals and UUIDs as strings

The CSV starts with a UTF-8 byte-order mark because Excel opens a CSV without one in the system's legacy code page, which garbles every non-ASCII name. Most other tools skip it; in Python, read the file with encoding="utf-8-sig".

XLSX is offered only when openpyxl is installed:

pip install "hyper-admin[xlsx]"

Without it the menu shows CSV and JSON, and ?format=xlsx answers 404.

Large lists

The export reads the list in batches of 500 rows, by keyset (WHERE (sort column, primary key) > last row) when the sort allows it and by page otherwise, and writes each batch to the response as it arrives. The server never holds the whole table: memory stays flat however long the list is.

A file holds at most export_max_rows rows (default 100000). When the list has more, the Export menu says Only the first 100,000 of 250,000 rows are exported; on a cursor-paged list, which never counts its rows, it says that exports stop at the cap. Narrow the list with a filter to export the rest, or raise the cap for that model.

Spreadsheet formulas

A spreadsheet runs a cell that starts with =, +, - or @ as a formula, and some also act on a leading tab or carriage return. A record named =HYPERLINK("http://example.test","x") would then run when someone opens the file. In CSV and XLSX files, every text cell that starts with one of these characters gets a leading ', so it is shown as text. Numbers and dates are not text and are written as they are, so -5 stays a number. JSON is not opened by spreadsheets and is written unchanged.

The export URL

The menu links to {admin prefix}/{model}/export?format=csv followed by the list's own querystring, for example /admin/invoice/export?format=csv&search=acme&filter_status=paid&sort_by=total&sort_direction=desc. Paging parameters (page, page_size, cursor) are ignored. The response is an attachment named {model}-{UTC timestamp}.{extension} with Cache-Control: no-store.