Skip to main content

Tools to convert Django ORM models to Pydantic models

Project description

dantico

Tools to convert Django ORM models to Pydantic models.

GitHub Actions (Test) Codecov PyPI version Downloads PyPI Supported Python Versions PyPI Supported Django Versions


Documentation: https://xshapira.github.io/dantico/


The key features are:

  • Custom Field Support: Create Pydantic Schemas from Django Models with default field type validations out of the box.

  • Field Validator: Fields can be validated with Pydantic validator or root_validator.

Requirements

  • Python 3.7+
  • Django 3.0+
  • Pydantic 1.6+

Installation

pip install dantico

Usage

Assume we have the following user model definition:

# models.py

from django.db import models


class User(models.Model):

    GENDER_MALE = "male"
    GENDER_FEMALE = "female"
    GENDER_OTHER = "other"

    GENDER_CHOICES = (
        (GENDER_MALE, "Male"),
        (GENDER_FEMALE, "Female"),
        (GENDER_OTHER, "Other"),
    )

    username = models.CharField(max_length=20)
    age = models.IntegerField()
    gender = models.CharField(
        choices=GENDER_CHOICES,
        max_length=10,
        blank=True,
    )
    password = models.CharField(max_length=100)
    company = models.ForeignKey(
        Company,
        on_delete=models.CASCADE,
    )
    languages = models.ManyToManyField(Language)

    def __str__(self):
        return self.name

Using the ModelSchema class will automatically generate schemas from our User model.

# schemas.py

from dantico import ModelSchema
from users.models import User


class UserSchema(ModelSchema):
    class Config:
        model = User


json_output = json.dumps(UserSchema.schema(), indent=4)
print(json_output)


# Output:
{
    "title": "UserSchema",
    "type": "object",
    "properties": {
        "id": {
            "title": "Id",
            "extra": {},
            "type": "integer"
        },
        "Username": {
            "title": "Username",
            "maxLength": 20,
            "type": "string"
        },
        "age": {
            "title": "Age",
            "type": "integer"
        },
        "gender": {
            "title": "Gender",
            "allOf": [
                {
                    "$ref": "#/definitions/GenderEnum"
                }
            ]
        },
        "password": {
            "title": "Password",
            "maxLength": 100,
            "type": "string"
        },
        "company_id": {
            "title": "Company",
            "type": "integer"
        },
        "languages": {
            "title": "Languages",
            "type": "array",
            "items": {
                "type": "integer"
            }
        }
    },
    "required": [
        "Username",
        "age",
        "password",
        "company_id",
        "languages"
    ],
    "definitions": {
        "GenderEnum": {
            "title": "GenderEnum",
            "description": "An enumeration.",
            "enum": [
                "male",
                "female",
                "other"
            ]
        }
    }
}

License

This project is licensed under the terms of the MIT license.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dantico-0.0.10.tar.gz (516.5 kB view details)

Uploaded Source

Built Distribution

dantico-0.0.10-py3-none-any.whl (15.8 kB view details)

Uploaded Python 3

File details

Details for the file dantico-0.0.10.tar.gz.

File metadata

  • Download URL: dantico-0.0.10.tar.gz
  • Upload date:
  • Size: 516.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.15

File hashes

Hashes for dantico-0.0.10.tar.gz
Algorithm Hash digest
SHA256 259afc579dafb352759ae1e867392536a99a2a009bb6c213952a973ab8d73512
MD5 3170a60696a34dd2a49f3f5c77800b14
BLAKE2b-256 f1c6a672c16177b92a81ae130d8739725ad4b6fe547d7d18d6c378f7fbcf670c

See more details on using hashes here.

File details

Details for the file dantico-0.0.10-py3-none-any.whl.

File metadata

  • Download URL: dantico-0.0.10-py3-none-any.whl
  • Upload date:
  • Size: 15.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.15

File hashes

Hashes for dantico-0.0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 d604a2a6846d0bf643aed235863160d78b21f9be7dc20aa72b8171c609a6dfd2
MD5 083e0ca8498e5a2f9ebab5dc441b789e
BLAKE2b-256 094e3187f2cf675768684bf462a335dd25b71eb70a47b4f7554b094a4256d727

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