Auto dataclass
AutoDataclass is a simple package that helps easy to map data into DataTransferObject for transporting that data between system layers. The package uses specified Dataclass structure for retrieving data and creating DTO. Currently, supported Django model object to Dataclass object conversion.
Installation
pip install auto_dataclass
Quickstart
# Django models.py
from django.db import models
class Product(models.Model):
name = models.CharField(max_length=128)
description = models.TextField(max_length=1000)
class Photo(models.Model):
product = models.ForeignKey(Product, related_name="photos",
on_delete=models.CASCADE)
image = models.ImageField(blank=True)
Define your Dataclasses that describe retrieved data structure from DB.
# dto.py
@dataclass(frozen=True)
class PhotoDataclass:
id: int
image: str
@dataclass(frozen=True)
class ProductDataclass:
id: int
name: str
description: str
photos: List[ProductDataclass] = field(default_factory=list)
Create Converter instance and call to_dto method with passed data from the query and previously defined Dataclass.
# repository.py
from auto_dataclass.dj_model_to_dataclass import FromOrmToDataclass
from dto import ProductDataclass
from models import Product
# Creating Converter instance
converter = FromOrmToDataclass()
def get_product(product_id: int) -> ProductDataclass:
product_model_instance = Product.objects \
.prefetch_related('photos') \
.get(pk=product_id)
# Converting Django model object from the query to DTO.
retrun converter.to_dto(product_model_instance, ProductDataclass)
Recursive Django model relation
If your data has a recursive relation you can also map them with the same way.
# Django models.py
from django.db import models
class Category(models.Model):
name = models.CharField(max_length=128)
parent = models.ForeignKey(
"Category", related_name="sub_categories", null=True, blank=True, on_delete=models.CASCADE
)
# dto.py
@dataclass
class CategoriesDTO:
id: int
name: str
sub_categories: List['CategoriesDTO'] = field(default_factory=list)
# repository.py
from itertools import repeat
from auto_dataclass.dj_model_to_dataclass import FromOrmToDataclass
from models import Category
from dto import CategoriesDTO
converter = FromOrmToDataclass()
def get_categories(self) -> Iterable[CategoriesDTO]:
category_model_instances = Category.objects.filter(parent__isnull=True)
return map(converter.to_dto, category_model_instances, repeat(CategoriesDTO))
Future Dataclass data types
Example of mapping future relation in Dataclasses structure.
# dto.py
@dataclass
class Dataclass:
id: int
dc: Optional['FutureDataclass']
@dataclass
class FutureDataclass:
id: int
name: str
To handle this you just need to pass a future Dataclasses as a next arguments.
from auto_dataclass.dj_model_to_dataclass import FromOrmToDataclass
from dto import Dataclass, FutureDataclass
converter = FromOrmToDataclass()
dataclass_with_future_relation = converter.to_dto(
django_data_model,
Dataclass,
FutureDataclass
)
Metadata
Release files for auto_dataclass 0.1.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| auto_dataclass-0.1.7.tar.gz | 9.3 kB | Without provenance | Details for auto_dataclass-0.1.7.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| auto_dataclass-0.1.7-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details for auto_dataclass-0.1.7-py2.py3-none-any.whl |
Total release size: 14.4 kB
Release files / auto_dataclass-0.1.7.tar.gz
| Download URL | auto_dataclass-0.1.7.tar.gz |
|---|---|
| Size | 9.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7e77afeb766a43477ca115123611ad0cf7590f7aaf614d8a76771a8cea5f1a70
|
|
BLAKE2b-256 checksum How to use checksums |
a1d44207c4b6773b9e3592f4fbfa76f0e8036b5ec5f8f8a613864066d2b38289
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-requests/2.31.0
|
Release files / auto_dataclass-0.1.7-py2.py3-none-any.whl
| Download URL | auto_dataclass-0.1.7-py2.py3-none-any.whl |
|---|---|
| Size | 5.1 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
20d69252ce4ea929a3a3ae41ebe2d75b8ebf5d723327ad39c399c35d579cf3a2
|
|
BLAKE2b-256 checksum How to use checksums |
cc0ec5b9ac7c8020d683e84a62c01382550a1cff659895f5bf5ca8ab75da9861
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-requests/2.31.0
|
Security for auto_dataclass 0.1.7
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.