Storing DTO data in easy-to-use and production-ready Django Model Field with fast [de]serialization.
Project description
django-dto-field
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
dictas DTO - Supports
dataclassas DTO - Supports
pydanticas DTO - Supports
marshmallowas DTO - Supports
attrsas DTO - Supports
adapdtixas 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).
- Set
DTOFieldto your model:
>>> from django.db.models import CharField
>>> from django_dto_field import DTOField
>>> class Country(Model):
... name = CharField()
... city = DTOField()
- 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.
- Set
dataclassschema:
>>> from dataclasses import dataclass
>>> @dataclass
... class City:
... name: str
... districts: list[str]
- Set
DTOFieldto 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)
- 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:
- Define the new
DTOFieldwith your new schema. - Write new custom Django migration to get, change and save updated data to new field.
- Delete old field.
🤗 Author
Made with love by @skv0zsneg
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_dto_field-0.1.1b1.tar.gz.
File metadata
- Download URL: django_dto_field-0.1.1b1.tar.gz
- Upload date:
- Size: 5.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.27 {"installer":{"name":"uv","version":"0.11.27","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}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e935d28db24a29e7efbd257deece2768dfd3dd20556cf3a39f9f49499a6306c0
|
|
| MD5 |
c81002e7c5da2915a9a055939a83aa7b
|
|
| BLAKE2b-256 |
e1dfc7e5ead9d43b7c0ba7d18167bdef9c3c519f26bdb2bb3d084f6345cc531a
|
File details
Details for the file django_dto_field-0.1.1b1-py3-none-any.whl.
File metadata
- Download URL: django_dto_field-0.1.1b1-py3-none-any.whl
- Upload date:
- Size: 8.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.27 {"installer":{"name":"uv","version":"0.11.27","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}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9f82e84f93f0b05230af4d1518b7fcfd8cc1813527737ae6d63d9ff3b926ede3
|
|
| MD5 |
e5038fad5a0e0ea246f5ee588ed9e9c5
|
|
| BLAKE2b-256 |
1aa6485c42450f5f36a0743114dacea5265ef99465be30f914b9fb3eecca0d20
|