Skip to main content


django-dto-field logo

PyPi Typing and Linters PyPi
wemake style wemake style

Features

  • Python dict support by default
  • Python dataclasses support
  • Pydantic models
  • Marshmallow schemas
  • attrs defines
  • Storing in BinaryField
  • Storing in CharField
  • Storing in JSONField
  • Lookups for JSONField
  • JSONField forms
  • Supported MySQL DB
  • Supported Postgres DB
  • Supported MariaDB

Quick start

Installing:

pip install django-dto-field

Usage:

>>> from dataclasses import dataclass
>>> from django.db import models
>>> from django_dto_field.fields.json import DTOJSONField


>>> @dataclass  # Define DTO
... class Address:
...    city: str

>>> @dataclass
... class Customer:  # Use nested DTO
...    name: str
...    address: Address


>>> class Order(models.Model):  # Add field to model
...    customer = DTOJSONField(schema=Customer)

>>> order = Order.objects.create(  # Save it
...    customer=Customer("Alice", Address("London"))
... )


>>> order.refresh_from_db()  # Get it
>>> assert order.customer == Customer("Alice", Address("London"))
>>> assert isinstance(order.customer.address, Address)

>>> Order.objects.filter(customer__address__city="London")  # Use lookups
>>> Order.objects.values_list("customer__address", flat=True)  # Return dictionary

Fields

Use schema=Customer to store the dataclass from the quick start. Each field accepts its Django base field's options, such as null and blank.

Field Django base Stored value
DTOJSONField JSONField Native JSON
DTOCharField CharField JSON text
DTOBinaryField BinaryField UTF-8 JSON bytes

The quick start uses DTOJSONField. To store the same DTO as text or bytes:

>>> from django_dto_field.fields.char import DTOCharField
>>> from django_dto_field.fields.binary import DTOBinaryField

>>> class StoredOrder(models.Model):
...     customer_text = DTOCharField(schema=Customer, max_length=500)
...     customer_bytes = DTOBinaryField(schema=Customer)

DTOJSONField supports Django JSON key lookups; projected keys return native values, not DTOs. DTOCharField requires max_length, which limits the serialized JSON text. DTOBinaryField is not editable by default, as with Django's BinaryField.

JSON key projections follow Django's backend behavior. On SQLite, use KeyTextTransform when strings such as "0" or "null" must remain strings. For optional form input, use null=True, blank=True, or submit {} for an empty dictionary; blank=True alone does not allow SQL NULL.

Schema validation runs on assignment by default, including model construction. Set validate_on_assignment=False on a field to defer validation until conversion or saving. In-place changes to a DTO or dictionary are checked on save. SQL expressions bypass literal-value validation; DTO results are checked on load. Call full_clean() to run Django field validators; they receive the stored mapping, text, or bytes.

Default dict DTO

Omit schema on any DTO field to store dictionaries with JSON-compatible values. Loading returns a dict.

>>> class Event(models.Model):
...     details = DTOJSONField(default=dict)

>>> event = Event.objects.create(details={"kind": "signup"})
>>> event.refresh_from_db()
>>> event.details
{'kind': 'signup'}

Supported versions and databases

  • Python 3.10–3.14.
  • Django 5.2 (Python 3.10–3.14) and 6.0–6.1 (Python 3.12–3.14).
  • SQLite, PostgreSQL, MySQL, and MariaDB.

Help and contribute

Have a question, spotted a bug, or want a new DTO integration? We'd love to hear from you in the issue tracker. For bugs, include a minimal example, your Python/Django versions, and the expected and actual behavior.

Read CONTRIBUTING.md for setup, testing, and pull request guidance. Small improvements count too—clearer examples, documentation fixes, and tests are all welcome. The project uses unit, property-based, and Django integration tests with 100% statement/branch coverage, plus Ruff, WPS, and strict mypy checks.

License

Available under the MIT license.

Metadata

Release files for django-dto-field 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 django-dto-field 0.2.0
File Size Uploaded Provenance Actions
django_dto_field-0.2.0.tar.gz 10.2 kB Without provenance Details for django_dto_field-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-dto-field 0.2.0
File Interpreter ABI Platform Actions
django_dto_field-0.2.0-py3-none-any.whl Python 3 none any Details for django_dto_field-0.2.0-py3-none-any.whl

Total release size: 27.8 kB

Release files / django_dto_field-0.2.0.tar.gz

Download URL django_dto_field-0.2.0.tar.gz
Size 10.2 kB
Tags Source
SHA-256 checksum
How to use checksums
0efb6437b8bdcf4c6af05c7405c621c9cb8d58c649c71a684a083131fef8b9f0
BLAKE2b-256 checksum
How to use checksums
1b72586ea9bd6041616213945347c435f716fc457d0fc417aefe8ccb80a3dbd9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.24 {"installer":{"name":"uv","version":"0.12.24","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

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

Download URL django_dto_field-0.2.0-py3-none-any.whl
Size 17.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
64f288cf32fa38bae0f267dab25147f7fb3ce93c3dd767fe8bde5ac4adbf250f
BLAKE2b-256 checksum
How to use checksums
3cd74f29ceffc33357340ebe4229ac8ae9ddc0a43b54ae810bac7355cf7b9b46
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.24 {"installer":{"name":"uv","version":"0.12.24","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for django-dto-field 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.

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