Skip to main content

The package, helps to automatically transform data to DTO (DataClass) object

Project description

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
)

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

auto_dataclass-0.1.6.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

auto_dataclass-0.1.6-py2.py3-none-any.whl (5.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file auto_dataclass-0.1.6.tar.gz.

File metadata

  • Download URL: auto_dataclass-0.1.6.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.25.1

File hashes

Hashes for auto_dataclass-0.1.6.tar.gz
Algorithm Hash digest
SHA256 bd843774d5964a32cdc86a16fb95f5744b21237e7b8edfd7a11acd9c0657c24b
MD5 0f6d2defa9d992abe9bef24b3a6f69c4
BLAKE2b-256 99b9724c3783677133acf552c89e9a5f1c92839ce41bddb0c4be932e7c547936

See more details on using hashes here.

File details

Details for the file auto_dataclass-0.1.6-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for auto_dataclass-0.1.6-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 7109feb47a475f2fbc1bebf70daaa9c7b59314ca7f6fb6feed912d56808d950a
MD5 16fea58c398de5596982d16496c42de8
BLAKE2b-256 d7187101927721f873d65870993b4649c0072d1e3ab02a6b297c680b1df6e86a

See more details on using hashes here.

Supported by

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