Skip to main content

drf-parcel

PyPI Python Django CI Docs License

One response shape for a Django REST framework API - success, page and error alike - and the generated schema saying exactly that shape.

A standard JSON response format for DRF: every success, every page - by number or by cursor - and every error in the same envelope, from one exception handler, with drf-spectacular describing it in the OpenAPI schema.

فارسی

{"success": true,  "status_code": 200, "message": "Done.",    "data": {"id": 7}}
{"success": true,  "status_code": 200, "message": "Done.",    "data": [], "pagination": {"type": "page", "count": 45, "page": 2, "pages": 3, "page_size": 20, "has_next": true, "has_previous": true}}
{"success": false, "status_code": 409, "message": "...",      "data": {"branches": 3}, "error_code": "province_in_use"}

A client reads one shape and never special-cases a payload: the answer is under data, a person reads message, and code branches on error_code - never on the message, which is translated.

The documentation - a quickstart, a tutorial, how-to guides, the reference - in English and Persian.

In a project

pip install 'drf-parcel[schema]'      # the schema extra brings drf-spectacular
INSTALLED_APPS = [
    ...,
    'rest_framework',
    'drf_parcel',
]

REST_FRAMEWORK = {
    'EXCEPTION_HANDLER': 'drf_parcel.handlers.exception_handler',
    'DEFAULT_PAGINATION_CLASS': 'drf_parcel.pagination.PageNumberPagination',
    'DEFAULT_SCHEMA_CLASS': 'drf_parcel.schema.ParcelAutoSchema',
}

Each line is independent: the handler alone gives the error envelope and nothing else, and a project adopts the rest one view at a time.

from drf_parcel.pagination import CursorPagination
from drf_parcel.responses import EnvelopeMixin, success_response


class ProductViewSet(EnvelopeMixin, ModelViewSet):
    queryset = Product.objects.all()
    serializer_class = ProductSerializer
    page_size = 12
    client_page_size = True          # the client may send ?page_size=
    max_page_size = 48               # and never gets more
    envelope_messages = {'create': _('The product was added.')}


class NotificationViewSet(EnvelopeMixin, ModelViewSet):
    pagination_class = CursorPagination
    ordering = '-created_at'


class TotalsView(EnvelopeMixin, APIView):
    def get(self, request):
        return success_response({'total': 12}, message=_('Totals.'))

EnvelopeMixin wraps what a view answers once, in finalize_response, so no action - list, create, a custom @action - has to remember. A delete answers 200 with the envelope rather than a 204 with nothing. A file, a stream, a redirect or a CSV is left alone, and so is a view with parcel_wrap = False.

A shop that uses every part of it, to run and read, is in example/.

Errors

from drf_parcel.exceptions import DomainError


class ProvinceInUse(DomainError):
    status_code = 409
    default_code = 'province_in_use'
    default_detail = _('This province has branches and cannot be deleted.')


raise ProvinceInUse(data={'branches': 3})

A DomainError is DRF's APIException with data added, so a project's existing errors answer in the envelope unchanged. The handler also:

  • says a validation error's first problem in message and every problem in data.fields, each with its code at a flat path - items.0.quantity;
  • answers DRF's and Django's own failures with a standard code - not_authenticated, permission_denied, not_found, rate_limited with retry_after ... - and keeps the headers DRF puts on them;
  • answers anything unexpected with a generic 500 whose text never leaves the server: it is logged with its traceback and handed to your REPORTER. Under DEBUG it is raised, so Django's page shows it;
  • undoes the request's write behind every failure it answers.

Pagination

Numbered pages or a cursor, chosen per view. pagination.type says which, so one component on a client reads either. A client chooses the page size only where a view allows it, and always under a ceiling.

The schema

With ParcelAutoSchema - or ParcelSchemaMixin in your own AutoSchema - the OpenAPI document says what the wire carries: every success inside the envelope, each page as its own envelope, page_size only where it is accepted, and the error envelope as every operation's default answer.

Settings

One dictionary, DRF_PARCEL. A key the library does not know stops the project at start-up rather than being ignored, and manage.py check reports a value that cannot work. Every key, every shape and every error code is in the contract.

Languages

Every sentence the library says is in the request's language. It ships Persian; DRF's own messages come in DRF's languages.

Supported

Python 3.11 to 3.14; Django 5.2, 6.0 and 6.1; Django REST framework 3.16 and later; drf-spectacular 0.28 and later, for the schema.

Working on it

Every check runs through nox, the same sessions locally and in CI.

uv tool install nox
nox -s lint types tests

tests runs every supported Python and Django; oldest runs the oldest release of everything supported; package builds the wheel and checks what is in it. The suite runs on SQLite by default, and on PostgreSQL with DRF_PARCEL_TEST_DATABASE=postgres and the standard PG* variables.

After changing a marked string, translate it in src/drf_parcel/locale/fa/LC_MESSAGES/django.po and compile it:

nox -s messages

Licence

BSD 3-Clause - see LICENSE.

Metadata

Release files for drf-parcel 0.2.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 drf-parcel 0.2.0
File Size Uploaded Provenance Actions
drf_parcel-0.2.0.tar.gz 118.5 kB With provenance Details for drf_parcel-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for drf-parcel 0.2.0
File Interpreter ABI Platform Actions
drf_parcel-0.2.0-py3-none-any.whl Python 3 none any Details for drf_parcel-0.2.0-py3-none-any.whl

Total release size: 149.2 kB

Release files / drf_parcel-0.2.0.tar.gz

Download URL drf_parcel-0.2.0.tar.gz
Size 118.5 kB
Tags Source
SHA-256 checksum
How to use checksums
56b5947ac0b5895970ac3da597f393ec7dabd457532b730a8f3ce23dd7dabdba
BLAKE2b-256 checksum
How to use checksums
66d654118374c713560a9280d12be7db9c1aa35d4e4b99b0227ee4ab23f4c4f9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 15, 2026.

Transparency log

Release files / drf_parcel-0.2.0-py3-none-any.whl

Download URL drf_parcel-0.2.0-py3-none-any.whl
Size 30.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c8d695a6166f4cd8d8cc56862622dcda509e1845708021b9cdace7d841e5704c
BLAKE2b-256 checksum
How to use checksums
e7d9d1e43ac387184361530f42136010daa7df73b157d1945fec644d2df9d50d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 15, 2026.

Transparency log

Security for drf-parcel 0.2.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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for drf_parcel-0.2.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 15, 2026.

Transparency log

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.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files Full provenance

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