Pydantic model support for Django ORM
Project description
Pydantic-Django
Pydantic model interface for Django ORM.
Important: this project should be considered an experimental work-in-progress. The current API design and behaviour is not finalised and specific version support is not yet determined.
Installation
pip install pydantic-django
Usage
Requirements: Python 3.7+, Django 2+
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", "type": "integer"},
"id": {"title": "Id", "type": "integer"},
"first_name": {"title": "First Name", "maxLength": 50, "type": "string"},
"last_name": {"title": "Last Name", "maxLength": 50, "type": "string"},
"email": {"title": "Email", "maxLength": 254, "type": "string"},
"created_at": {
"title": "Created At",
"type": "string",
"format": "date-time",
},
"updated_at": {
"title": "Updated At",
"type": "string",
"format": "date-time",
},
},
"required": ["first_name", "email", "created_at", "updated_at"],
}
There are a few ways to populate the models with values, the first is using the from_django
method:
user = User.objects.create(
first_name="Jordan",
last_name="Eremieff",
email="jordan@eremieff.com"
)
user_schema = UserSchema.from_django(user)
Alternatively, the Pydantic model can be used to create a new object:
user_schema = UserSchema.create(
first_name="Jordan",
last_name="Eremieff",
email="jordan@eremieff.com"
)
Or retrieve an existing one:
user_schema = UserSchema.get(id=user.id)
The object in each case can be validated and export the values in the same way:
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"
}
It can also use standard Python type annotations in conjunction with the fields retrieved automatically from the database, and the configuration class supports exclude
and include
options:
class UserSchema(ModelSchema):
first_name: Optional[str]
last_name: str
class Config:
model = User
include = ["first_name", "last_name"]
In this example, the first name and last name annotations override the fields that would normally be picked up from the Django model automatically, and the include
list filters out the other fields from the schema definition.
The first_name
field here is required in the database and the last_name
field is optional, but using the type annotations this can be determined for the specific schema:
{
'description': 'A user of the application.',
'properties': {
'first_name': {'title': 'First Name', 'type': 'string'},
'last_name': {'title': 'Last Name', 'type': 'string'}
},
'required': ['last_name'],
'title': 'UserSchema',
'type': 'object'
}
It can do a bit more than this, but you'll have to check out the testing application and test cases as a reference for now.
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
File details
Details for the file pydantic-django-0.0.8.tar.gz
.
File metadata
- Download URL: pydantic-django-0.0.8.tar.gz
- Upload date:
- Size: 16.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/3.8.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c6612c51a85b5b93c06bcb7988a96415bbf8b78db1eaf788d125ce870fdb4b2d |
|
MD5 | a7921ec2fadf8114db829870649e09bd |
|
BLAKE2b-256 | eedc3b542387937181fa007371395581d14fdbaa55871b5ca1029f3012a3b22f |
File details
Details for the file pydantic_django-0.0.8-py3-none-any.whl
.
File metadata
- Download URL: pydantic_django-0.0.8-py3-none-any.whl
- Upload date:
- Size: 23.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/3.8.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4735e1caaf2272662a8fc0b72a15fa8a75811b2e15cbbd3cb5509ecaa29669c6 |
|
MD5 | 17bc20ba216c12b4e80ce3375a2f49fa |
|
BLAKE2b-256 | 1822e75918ff864982b42d38fccdd6e00e4ab65db244da0501adff32eb6d586d |