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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for pydantic_django-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d2d16f47872af4590c40c7c4aaf1ba7e515e493c14a9d599d53717580a2c53e8 |
|
MD5 | 0db9318b767fcdcacbbd52e0fdcff61c |
|
BLAKE2b-256 | 3d08ee6a62b68370c2fe5132666d8bc85293481383e6305d44ce6874e8ca4dbb |