Skip to main content

Pydantic model support for Django ORM

Project description

Djantic

PyPI version

Documentation: https://jordaneremieff.github.io/djantic/

Requirements: Python 3.7+, Django 3.0+

Pydantic models for Django.

This project should be considered a work-in-progress. It should be okay to use, but no specific version support has been determined (#16) and the default model generation behaviour may change across releases.

Please use the issues tracker to report any bugs, or if something seems incorrect.

Quickstart

Install using pip:

pip install djantic

Generating schemas from models

Configure a custom ModelSchema class for a Django model to generate a Pydantic model. This will allow using the Django model information with Pydantic model methods:

from users.models import User
from djantic import ModelSchema

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

print(UserSchema.schema())

Output:

{
        "title": "UserSchema",
        "description": "A user of the application.",
        "type": "object",
        "properties": {
            "profile": {"title": "Profile", "description": "None", "type": "integer"},
            "id": {"title": "Id", "description": "id", "type": "integer"},
            "first_name": {
                "title": "First Name",
                "description": "first_name",
                "maxLength": 50,
                "type": "string",
            },
            "last_name": {
                "title": "Last Name",
                "description": "last_name",
                "maxLength": 50,
                "type": "string",
            },
            "email": {
                "title": "Email",
                "description": "email",
                "maxLength": 254,
                "type": "string",
            },
            "created_at": {
                "title": "Created At",
                "description": "created_at",
                "type": "string",
                "format": "date-time",
            },
            "updated_at": {
                "title": "Updated At",
                "description": "updated_at",
                "type": "string",
                "format": "date-time",
            },
        },
        "required": ["first_name", "email", "created_at", "updated_at"],
    }

See https://pydantic-docs.helpmanual.io/usage/models/ for more.

Loading and exporting model data

Use the from_django method on a model schema class to load a Django model instance into a schema class:

user = User.objects.create(
    first_name="Jordan", 
    last_name="Eremieff", 
    email="jordan@eremieff.com"
)

user_schema = UserSchema.from_django(user)
print(user_schema.json(indent=2))

Output:

{
    "profile": null,
    "id": 1,
    "first_name": "Jordan",
    "last_name": "Eremieff",
    "email": "jordan@eremieff.com",
    "created_at": "2020-08-15T16:50:30.606345+00:00",
    "updated_at": "2020-08-15T16:50:30.606452+00:00"
}

See https://pydantic-docs.helpmanual.io/usage/exporting_models/ for more.

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

djantic-0.5.0.tar.gz (19.8 kB view details)

Uploaded Source

Built Distribution

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

djantic-0.5.0-py3-none-any.whl (24.8 kB view details)

Uploaded Python 3

File details

Details for the file djantic-0.5.0.tar.gz.

File metadata

  • Download URL: djantic-0.5.0.tar.gz
  • Upload date:
  • Size: 19.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.12

File hashes

Hashes for djantic-0.5.0.tar.gz
Algorithm Hash digest
SHA256 21c1340d0fc114e9788f99da426698b269eefa2d5cdca850b0c873e6675d3a48
MD5 06b34d7678876a777c6ed9cfea368a8e
BLAKE2b-256 974b381199527e414f6b3d33811e123708f367515dc6572ed2b4a5f52838ed41

See more details on using hashes here.

File details

Details for the file djantic-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: djantic-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 24.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.12

File hashes

Hashes for djantic-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3a5be779bf36e7609d39649ef50e47e4a694e155a27b17a053c43ff65579806c
MD5 394deb29f45e01f5f972320005a14396
BLAKE2b-256 9f4d7f4e5d1a8914258518814d2f871e0cc676d2a75eeed1236abcb89aadae9d

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