This project has been archived by its maintainers, and is no longer receiving any updates.
djdantic
A utility library to integrate and use pydantic with the django orm. This package includes optional sentry integration using sentry-tools.
🚧 This project is WIP and is subject to change at any time
This project is currently in the alpha state, even though it can be used in production with some caution. Make sure to fix the version in your requirements.txt and review changes frequently.
Installation
pip install djdantic
Features
pydantic to django Data Schema Conversion
djdantic.BaseModel
Providesfrom_ormmethod on pydantic schemadjdantic.Field
Provides a custom implementation of pydantic'sField, used for the custom optionsdjdantic.utils.pydantic_django.transfer_from_ormdjdantic.utils.pydantic_django.transfer_to_orm
If automatic route generation is used, it is not neccessary to use the transfer_* methods manually.
Options for djdantic.Field
For mapping pydantic schemas to django models, it is required to add at least one of the following extra arguments to each field defined in a pydantic schema.
Using these options on pydantic's
Fieldis also possible but deprecated!
orm_field:django.db.models.Field(required)
Pointer (reference) to the corresponding model field, e. g.myapp.models.MyModel.idorm_method:Optional[Callable[[Self], Any] | Callable[[Self, Any], None]]
Pointer to a orm model method, which is called when the object is loaded from the orm into a pydantic model or written into the orm from a pydantic model, e. g.myapp.models.MyModel.get_calculated_valuescopes:Optional[List[str]]
Limit access to specific fields based on jwt token scopes. For read operations, only scopes with the actionreadare taken into account, for write all other scopes are taken into account.is_critical:Optional[bool]
Limit write access to the field based on the presence of thecrtflag in the jwt token.is_sync_matching_field:Optional[bool]
Alternative tosync_matchingto define search fields for the matcher on the field itselfsync_matching:Optional[List[Tuple[str, django.db.models.Field]]]
Used for performing atransfer_to_ormwith actionTransferAction.SYNCfor included sub-records (in a list), used when noidfield is present on the object. Mapping from pydantic field (dot notation for nested fields can be used) to the corresponding django model field.
⚠️ Deprecated in favor ofis_sync_matching_field
Example for Schemas
from djdantic import Field, BaseModel
from ... import models
class User(BaseModel):
email: str = Field(orm_field=models.User.email)
is_password_usable: bool = Field(orm_method=models.User.has_usable_password)
is_superuser: bool = Field(scopes=['access.users.update.any'], is_critical=True, orm_field=models.User.is_superuser)
class UserUpdate(User):
password: Optional[SecretStr] = Field(orm_method=models.User.set_password, is_critical=True)
class OrderUpdate(BaseModel):
items: Optional[List['OrderItemUpdate']] = Field(
orm_field=models.Order.items,
sync_matching=[
('product', models.OrderItem.product),
],
)
Metadata
Release files for djdantic 0.0.24
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| djdantic-0.0.24.tar.gz | 26.0 kB | Without provenance | Details for djdantic-0.0.24.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| djdantic-0.0.24-py3-none-any.whl | Python 3 | none | any | Details for djdantic-0.0.24-py3-none-any.whl |
Total release size: 56.1 kB
Release files / djdantic-0.0.24.tar.gz
| Download URL | djdantic-0.0.24.tar.gz |
|---|---|
| Size | 26.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
528a960bc5ec0243f7c6719b55cea47597aec7cc0bb30c7e628e89d5a72454c7
|
|
BLAKE2b-256 checksum How to use checksums |
4b131d5d510abbdc51008a0ef40e15c9629faa9f066940ad29cd930438b8f670
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.5
|
Release files / djdantic-0.0.24-py3-none-any.whl
| Download URL | djdantic-0.0.24-py3-none-any.whl |
|---|---|
| Size | 30.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
14048af6b16bd7b1a501f42ab05e0b7302acc6461791e201bdb77b2dc7f11e37
|
|
BLAKE2b-256 checksum How to use checksums |
aa49662cd6e876a1a40fcb82975bdffa253dfe2a65c56db65d3a35fd9f2e9792
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.5
|
Security for djdantic 0.0.24
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.