Skip to main content

django-parquet-archive

PyPI Python Django CI License

Archive old rows of Django tables: they are written to parquet files (S3 or any Django storage) and deleted from the database. Tables are configured in the admin, and the archived rows can be browsed in the model's own changelist.

The archive runs as a celery task.

Supported versions

Django Python
5.2 3.10 to 3.14
6.0 3.12 to 3.14
6.1 3.12 to 3.14

Installation

pip install "django-parquet-archive[s3]"

The s3 extra installs django-storages[s3]. Without it the library works with any Django storage, but reads the parquet files by downloading them through the storage instead of querying the bucket directly.

Configuration

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

# A STORAGES alias (default 'default') or the import path of a storage object of the project
PARQUET_ARCHIVE_STORAGE = 'parquet_archive'
STORAGES['parquet_archive'] = {
    'BACKEND': 'storages.backends.s3boto3.S3Boto3Storage',
    'OPTIONS': {'bucket_name': 'my-archive-bucket'},
}

# Optional: files an admin page with a search or other filters reads at most (default 200)
PARQUET_ARCHIVE_MAX_FILES = 200

Schedule it with celery beat (the queue and the frequency are up to the project):

CELERY_BEAT_SCHEDULE = {
    'Archive old table rows': {
        'task': 'parquet_archive.archive_tables',
        'schedule': crontab(day_of_month='5', hour='0', minute='0'),
    },
}

Then run python manage.py migrate parquet_archive.

Archived tables

Each table is an ArchivePolicy, created in the admin under Parquet archive → Archive policies. The form lists the fields of the chosen model:

  • Date fields: a row is archived when any of them is older than the configured age. Includes fields of direct FKs (e.g. order__cancelled_at).
  • Keep for: age in days, months or years.
  • Ordering: the order of the rows in the files, and the field is always saved. Ordering by a date field of the model itself that is not null (e.g. -requested_at) makes it the index field: each file covers a short stretch of it, and the admin reads only the files of each page (see Browsing in the admin). In a policy created by a migration, include the field in the schema.
  • Saved columns: the parquet columns. The primary key is always saved. JSON is written as text and DurationField as a duration. Fields with from_db_value (encrypted fields, custom value objects...) are not offered: the value read from the database is a Python object, and an encrypted value would be archived decrypted.

Only superusers create, change, delete or run policies, because they define which rows are deleted from the database. Other users with the view permission see the saved policy. Every change made in the admin is recorded in the admin's own history, with the user and the changed fields.

The form rejects models that other models reference with CASCADE, PROTECT or RESTRICT, or that have a ManyToMany: deleting the rows after archiving them would delete data that is not archived, or would fail after the parquet files were written.

To create the initial policies with a migration in the project:

def create_policies(apps, schema_editor):
    ContentType = apps.get_model('contenttypes', 'ContentType')
    ArchivePolicy = apps.get_model('parquet_archive', 'ArchivePolicy')
    content_type, _ = ContentType.objects.get_or_create(app_label='core', model='apirequest')
    ArchivePolicy.objects.get_or_create(content_type=content_type, defaults={'config': {
        'rows_per_file': 3000,
        'filter': {'fields': ['requested_at'], 'keep_for': 3, 'unit': 'months'},
        'ordering': ['-requested_at'],
        'schema': {'id': 'String', 'requested_at': 'Datetime', 'status_code': 'Int64'},
    }})


class Migration(migrations.Migration):
    dependencies = [('parquet_archive', '0001_initial')]
    operations = [migrations.RunPython(create_policies, migrations.RunPython.noop)]

The schema types are String, Int64, Float64, Boolean, Date, Datetime, Duration, Json and Decimal(precision,scale). Use the type the admin shows for each field in the form: a policy whose types differ from those of its fields is not archived (see Changes in the model).

Each file has up to rows_per_file rows, which are deleted from the database by their primary keys right after the file is written. If a run fails halfway, the next one picks up what is left without duplicating the archive, and nothing that started to match the filter during the run is deleted without being in a file.

Files are stored as <lowercase model name>/<UTC timestamp>_<uuid>.parquet, and each one is registered in the index of the archive files right after it is written.

Each file is read with its own column types, and the files of a page are joined into a type that fits all of them (for example, the decimal with more digits, or text), so the files written before a field changed type are still read. A date column of a field that is now a DateTimeField becomes the start of its day, and a datetime column of a field that is now a DateField becomes its day, both in the project time zone. The admin lists the columns of the newest file (by the timestamp in the file name): columns added to the policy later show up, and are empty in older files.

Changes in the model

A policy never changes by itself. When the saved policy no longer matches its model, that table is not archived, with an error in the log, and the other tables are still archived:

  • a saved column whose field changed type (e.g. a DecimalField with more digits): writing with the saved type could round the values;
  • a saved column whose field is no longer in the model: a renamed field looks the same as a removed one, and archiving without the column would lose its data;
  • a field of the filter or of the ordering no longer in the model: saving the policy, the admin asks for another one;
  • a field whose type the archive does not support;
  • the model removed from the project. In the admin the policy keeps its configuration and can be deactivated.

Saving the policy again in the admin, which builds it from the current fields, fixes it, and the admin history records who changed it. A field added to the model is only saved once it is selected in the policy.

The archive task fails at the end with an ArchiveError that lists the tables that were not archived. A celery soft time limit is not the failure of one table: it stops the run right away. The database system check parquet_archive.W001 warns about these policies, without stopping anything, in migrate (before the migrations), in the test runner and in manage.py check --database default.

Index of the archive files

The admin lists the archive from an index in the database (ArchiveFile): the rows of each file and the first and last value of the index field. The archive task registers every file it writes. Files written before the index existed, or by another process into the same folders, are registered by the indexing, which reads each one once.

There is no manual step: the first time the archive of a table is listed in the admin, if the table was never indexed (e.g. right after the upgrade) or has files indexed by another field (its ordering changed), the admin queues the indexing task (parquet_archive.index_files, which needs a celery worker) and warns that the listing may be missing files for a few minutes. It is queued once; a request pending for more than an hour is queued again. The indexing can also run at any time:

  • in the admin, Parquet archive → Archive policies, action Index the archive files of the selected tables (superusers only);
  • or the celery task parquet_archive.index_files, which also takes model_names and can be scheduled to pick up files changed in the storage by other processes.

The indexing also removes the files that are no longer in the storage. A file that cannot be read is skipped with an error in the log. Files without the index field (written before it was saved) are left out of the listing, with a warning.

A file removed from the storage outside the library is still in the index until the next indexing: the admin pages that read it show an error message instead of the rows, and the detail of its rows goes back to the list with the message.

Browsing in the admin

from parquet_archive.admin import ArchiveAdminMixin


class ApiRequestAdmin(ArchiveAdminMixin, admin.ModelAdmin):
    ...

The mixin adds the Data source filter (Database / Archive). In the archive, the changelist reads the parquet files and keeps list_display, pagination, column sorting, field filters (FKs included) and search. The link of each row opens a read-only detail page.

Disabled in the archive: actions, custom filters (SimpleListFilter), facets and date_hierarchy. Filters and search fields that do not exist in the parquet files are ignored with a warning on the page.

Text in a field that is now a number or a boolean (written before the field changed type) is converted to it, so filters and ordering compare numbers; the primary key is the exception, and stays text when some file has it as text. A value that the model field does not convert (e.g. abc in a field that is now an integer) is shown as it is in the file, or left empty in the list when the field cannot show it (e.g. text in a BooleanField, or in a foreign key). The detail of the row shows these values as they are in the file, and also the columns of fields removed from the model, as text.

In tables with an index field, each page reads only the files it needs:

  • the filter of the index field becomes a dedicated filter, with the months and, for the month shown, the days that the archive files cover (the shortcuts of the default date filter count from today, and the archive only has old rows). In tables with few rows per day a file covers several days, and days without rows may show up;
  • without other filters, search or ordering by another column, the count comes from the index and each page reads only its files, in any period. Without an ordering in the ModelAdmin or in the model, the archive is listed by the index field, newest first;
  • with a search, other filters or ordering by another column, the page reads the files of the period. When they are more than PARQUET_ARCHIVE_MAX_FILES, it shows the rows of the most recent ones, with a link to continue before them;
  • the link of each row carries its date (?at=...), and the detail page reads only the files whose range has it.

Tables without an index field read every file on every page.

Translations

The admin texts are in English, with a Brazilian Portuguese (pt_BR) translation included.

Development

uv sync
uv run pytest
# Minimum supported Django version
uv run --with 'django~=5.2.0' pytest

CI runs the suite on every supported combination of Python and Django, and a Django deprecation caused by the library code fails the suite.

Releasing

Bump version in pyproject.toml and publish a GitHub release with the v<version> tag. The publish workflow checks that the tag matches the version, builds and publishes to PyPI with trusted publishing, with no token stored in the repository, and attaches the distributions to the release.

License

BSD-3-Clause.

Metadata

Release files for django-parquet-archive 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-parquet-archive 1.1.0
File Size Uploaded Provenance Actions
django_parquet_archive-1.1.0.tar.gz 63.3 kB Without provenance Details for django_parquet_archive-1.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-parquet-archive 1.1.0
File Interpreter ABI Platform Actions
django_parquet_archive-1.1.0-py3-none-any.whl Python 3 none any Details for django_parquet_archive-1.1.0-py3-none-any.whl

Total release size: 113.1 kB

Release files / django_parquet_archive-1.1.0.tar.gz

Download URL django_parquet_archive-1.1.0.tar.gz
Size 63.3 kB
Tags Source
SHA-256 checksum
How to use checksums
4ddb817cff3e159440e8ef31fc859b7dba26e4fb6e4fd630679ea3c953a9ce36
BLAKE2b-256 checksum
How to use checksums
0b478c5b164a01f615a56222a558bef7456e08f1e57156a24679b68223b88acd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.10 {"installer":{"name":"uv","version":"0.13.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / django_parquet_archive-1.1.0-py3-none-any.whl

Download URL django_parquet_archive-1.1.0-py3-none-any.whl
Size 49.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
59d71733aba453e69282edd153825bdd67c07f428d184d52b8cf3c0dff31ae3d
BLAKE2b-256 checksum
How to use checksums
6aea599449d3c6bc76fe2e8a8b886991890d3a133e58719033131f3ae1e1d827
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.10 {"installer":{"name":"uv","version":"0.13.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for django-parquet-archive 1.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page