Skip to main content

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

Project description

Auto dataclass

ci_badge Downloads PyPI version License: MIT

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.7.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

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

auto_dataclass-0.1.7-py2.py3-none-any.whl (5.1 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

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

File hashes

Hashes for auto_dataclass-0.1.7.tar.gz
Algorithm Hash digest
SHA256 7e77afeb766a43477ca115123611ad0cf7590f7aaf614d8a76771a8cea5f1a70
MD5 e962295bd5827fe05bd9cf6ffa6c6e38
BLAKE2b-256 a1d44207c4b6773b9e3592f4fbfa76f0e8036b5ec5f8f8a613864066d2b38289

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for auto_dataclass-0.1.7-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 20d69252ce4ea929a3a3ae41ebe2d75b8ebf5d723327ad39c399c35d579cf3a2
MD5 f5fb9d696e2a97f4d9b522cae9df89af
BLAKE2b-256 cc0ec5b9ac7c8020d683e84a62c01382550a1cff659895f5bf5ca8ab75da9861

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