Skip to main content

Storing DTO data in easy-to-use and production-ready Django Model Field with fast [de]serialization.

Project description

django-dto-field

PyPI version PyPI license Python Version

wemake-python-styleguide test typing & lint

Storing DTO data in easy-to-use and production-ready Django Model Field with fast [de]serialization.

A custom Django Model Field that can serialize and deserialize for different types of DTO (Data-Transfer-Object). DTOField detect DTO type and serialize the most efficiently. Also it has own binary container to maintain data integrity and economical storage.

✨ Features

  • All DB support (if DB support BinaryFiled)
  • Zip field for size saving (optional)
  • Supports pure dict as DTO
  • Supports dataclass as DTO
  • Supports pydantic as DTO
  • Supports marshmallow as DTO
  • Supports attrs as DTO
  • Supports adapdtix as DTO

⬇️ Install

$ pip install django-dto-field

🚀 Quick start

Choose your DTO: dict, dataclass

dict

We support pure dict for using it like DTO object. The main different between dict and other DTO types is that dict has no validation and DTOField uses it by default.

Also we have some benchmarks with default JSONField. And they shows that DTOField is 2.4x faster on write operation (thanks to amazing msgspec lib).

JSONField vs DTOField write

  1. Set DTOField to your model:
>>> from django.db.models import CharField
>>> from django_dto_field import DTOField

>>> class Country(Model):
...    name = CharField()
...    city = DTOField()
  1. Use it:
>>> city = {"name": "Capitol", "districts": [f"d{i}" for i in range(1, 14)]}
>>> created_country = Country.objects.create(name="Panem", city=city)

>>> from_db_country = Country.objects.get(pk=created_country.pk)
>>> assert isinstance(from_db_country.city, dict)
>>> assert from_db_country.city == city

dataclass

We support dataclass object as DTO. You can use it simply without thinking of serialization, deserialization and validation. DTOField will do it for you.

  1. Set dataclass schema:
>>> from dataclasses import dataclass

>>> @dataclass
... class City:
...     name: str
...     districts: list[str]
  1. Set DTOField to your model and add schema.
>>> from django.db.models import CharField
>>> from django_dto_field import DTOField

>>> class Country(Model):
...    name = CharField()
...    city = DTOField(schema=City)
  1. Use it:
>>> city = City(name="Capitol", districts=[f"d{i}" for i in range(1, 14)])
>>> created_country = Country.objects.create(name="Panem", city=city)

>>> from_db_country = Country.objects.get(pk=created_country.pk)
>>> assert isinstance(from_db_country.city, City)
>>> assert from_db_country.city == city

Validation error will raise if wrong schema will appear (what if schema change?):

>>> @dataclass
... class WrongCity:
...     addresses: list[str]
...

>>> wrong_city = WrongCity(addresses=[])
>>> from_db_country.city = wrong_city
Traceback (most recent call last):
    ...
ValidationError: given value 'WrongCity(addresses=[])' did not match schema 'City'

What if schema will change?

The goal of DTOField is to give you an easy tool for working with different DTO's objects but not how to manipulate them. This is why we will never give migration features.

But it's not a problem! Often you can make migration with 3 steps:

  1. Define the new DTOField with your new schema.
  2. Write new custom Django migration to get, change and save updated data to new field.
  3. Delete old field.

🤗 Author

Made with love by @skv0zsneg

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_dto_field-0.1.0b1.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_dto_field-0.1.0b1-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file django_dto_field-0.1.0b1.tar.gz.

File metadata

  • Download URL: django_dto_field-0.1.0b1.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.0 CPython/3.12.13 Linux/6.17.0-1010-azure

File hashes

Hashes for django_dto_field-0.1.0b1.tar.gz
Algorithm Hash digest
SHA256 6465de530323b020f9f0d1405c3ce31afaeedad7c9954d75870de5f9219323d2
MD5 32305f4bd506646dce8c69bbd34a2209
BLAKE2b-256 4fe60bfa678dfd803c2de5cde83e762f7a669329b18a2b25b81efb80bd4c395a

See more details on using hashes here.

File details

Details for the file django_dto_field-0.1.0b1-py3-none-any.whl.

File metadata

  • Download URL: django_dto_field-0.1.0b1-py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.0 CPython/3.12.13 Linux/6.17.0-1010-azure

File hashes

Hashes for django_dto_field-0.1.0b1-py3-none-any.whl
Algorithm Hash digest
SHA256 e6f54a6a1044a192a373d670fb2d1580e124a08637567d08af4f400486714783
MD5 221c06ea5b460da8dc5d00bab3fe65a8
BLAKE2b-256 6af2d47c7808b617f62c38e83a714298bfb079e3e3fd028846b0ff21a51d76b8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page