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.

  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.0a3.tar.gz (6.1 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.0a3-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_dto_field-0.1.0a3.tar.gz
  • Upload date:
  • Size: 6.1 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.0a3.tar.gz
Algorithm Hash digest
SHA256 472d982eb367d187d03eb951b82d316765cfe8c18265f3f1a1925d46f4b251cf
MD5 21a1fd58c0bde187d8f902714e6972d3
BLAKE2b-256 125bc7da5b447b42e889d706c2f26bd82087192327de74cc9d0e5f2cd73a27b7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: django_dto_field-0.1.0a3-py3-none-any.whl
  • Upload date:
  • Size: 9.0 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.0a3-py3-none-any.whl
Algorithm Hash digest
SHA256 c7bde78f9d5c7ae7aebd236684386da09ccfaa90a00670669a63819a7953335e
MD5 cbcb33729a03707f135b17d12d371c3e
BLAKE2b-256 b049cb1b694f260139a390771731c7b3825aba333f058695e14dd4fa5495b037

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