Skip to main content

Pydantic model support for Django ORM

Project description

Pydantic-Django

Pydantic model support for Django ORM.

Documentation: https://jordaneremieff.github.io/pydantic-django/

Requirements: Python 3.7+, Django 2+

Status

This project should be considered a work-in-progress. It should be okay to use, but no specific version support is guaranteed yet and expected outputs and behaviour may change as it continues to be developed.

There is a lot of Pydantic behaviour to cover, so please use the issues tracker for any bug report or if something seems wrong.

Quickstart

Install using pip:

pip install pydantic-django

An example of basic schema usage:

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

UserSchema.schema()

The schema call above would return something like this:

{
        "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"],
    }

Use the from_django method to populate the models with values:

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

user_schema = UserSchema.from_django(user)

The object values can be validated and serialized using the Pydantic export methods.

user_json = user_schema.json()

To produce a result such as:

{
    "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"
}

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

pydantic-django-0.1.0.tar.gz (16.0 kB view details)

Uploaded Source

Built Distribution

pydantic_django-0.1.0-py3-none-any.whl (22.2 kB view details)

Uploaded Python 3

File details

Details for the file pydantic-django-0.1.0.tar.gz.

File metadata

  • Download URL: pydantic-django-0.1.0.tar.gz
  • Upload date:
  • Size: 16.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.8

File hashes

Hashes for pydantic-django-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e06bae9c322df00c5c85123a0a395ea315a6ca3eb14582485f3503b333b6076b
MD5 f50e28bfb24fb11bda11d729c4f62a3d
BLAKE2b-256 12ff5c9ac8f9d330466273b778272eeebe44d263f346c11abe3cf658f31987b5

See more details on using hashes here.

File details

Details for the file pydantic_django-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pydantic_django-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 22.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.8

File hashes

Hashes for pydantic_django-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d2d16f47872af4590c40c7c4aaf1ba7e515e493c14a9d599d53717580a2c53e8
MD5 0db9318b767fcdcacbbd52e0fdcff61c
BLAKE2b-256 3d08ee6a62b68370c2fe5132666d8bc85293481383e6305d44ce6874e8ca4dbb

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