django-pydantic-field

Django JSONField with Pydantic models as a Schema

OTHER License

Downloads
50.3K
Stars
111
Committers
1

Bot releases are visible (Hide)

django-pydantic-field - v0.3.10 Latest Release

Published by surenkov 3 months ago

What's Changed

Full Changelog: https://github.com/surenkov/django-pydantic-field/compare/v0.3.9...v0.3.10

django-pydantic-field - v0.3.9

Published by surenkov 6 months ago

What's Changed

Full Changelog: https://github.com/surenkov/django-pydantic-field/compare/v0.3.8...v0.3.9

django-pydantic-field - v0.3.8: django-jsonform integration

Published by surenkov 6 months ago

What's Changed

Full Changelog: https://github.com/surenkov/django-pydantic-field/compare/v0.3.7...v0.3.8

django-pydantic-field - v0.3.7: Bring back compatibility with Pydantic v1

Published by surenkov 7 months ago

In 0.3.5, I introduced an incompatibility with Pydantic v1: particularly, importing pydantic_core unconditionally in compatibility layer 👀. Thank you @Abdullah0297445 for the bringing attention to this!

Both 0.3.5 and 0.3.6 were yanked from PyPI, I highly encourage to upgrade your pinned versions if you're the lucky one who has experienced this issue.

What's Changed

New Contributors

Full Changelog: https://github.com/surenkov/django-pydantic-field/compare/v0.3.6...v0.3.7

django-pydantic-field - v0.3.6

Published by surenkov 7 months ago

What's Changed

Full Changelog: https://github.com/surenkov/django-pydantic-field/compare/v0.3.5...v0.3.6

django-pydantic-field - v0.3.5

Published by surenkov 7 months ago

django-pydantic-field - v0.3.4

Published by surenkov 7 months ago

What's Changed

Full Changelog: https://github.com/surenkov/django-pydantic-field/compare/v0.3.3...v0.3.4

django-pydantic-field - v0.3.3

Published by surenkov 9 months ago

Make sure all expected primitives are exposed during dynamic module resolution in compatibility layer.

Allow (deprecated) django_pydantic_field.rest_framework.AutoSchema import with Pydantic 2.

What's Changed

Full Changelog: https://github.com/surenkov/django-pydantic-field/compare/v0.3.2...v0.3.3

django-pydantic-field - v0.3.2

Published by surenkov 9 months ago

What's Changed

New Contributors

Full Changelog: https://github.com/surenkov/django-pydantic-field/compare/v0.3.1...v0.3.2

django-pydantic-field - v0.3.2b1: django-constance compat

Published by surenkov 9 months ago

django-pydantic-field - v0.3.1

Published by surenkov 9 months ago

What's Changed

Full Changelog: https://github.com/surenkov/django-pydantic-field/compare/v0.3.0...v0.3.1

django-pydantic-field - v0.3.0: Pydantic v2 support

Published by surenkov 9 months ago

This release centers on the incorporation of v2 primitives. In the earlier version, Pydantic v1, support was achieved through a series of workarounds involving the manipulation of passed (or annotated) schemas. These schemas were wrapped into an intermediary RootModel to support both Pydantic models and most of the arbitrary annotations.

With v2, pydantic now ships a new primitive -- TypeAdapter -- which is well designed for this particular machinery.

Compatibility layer

To support both Pydantic v1 and v2, which might be useful during migration step, I added an indirection level during the import step.
0.3.0 contains implementations for both versions, with changes in v1 only required to make this compatibility layer to operate, with all behaviour remaining as in 0.2.13.

Lazy schema evaluation

In 0.2.* versions, exact schema resolution had been done in three stages:

  1. First, the field tried to resolve the passed schema during field descriptor creation.
  2. If it's failed, the next attempt had been performed during Django model class preparation, particularly in contribute_to_class method.
  3. If even step 2 had failed (because of forward references might be still unresolvable at this stage), then the schema evaluation had to be postponed until the first attempt to access the field on model instance (either instance creation, field assignment or so).

In this update, I decided that evaluation logic should be as simple as possible, thus keeping only stage 3, and removing all the dirty logic that supported the machinery around 1 and 2 stages.
To mitigate possible schema evaluation errors, which may appear only in runtime, the field now performs a few checks that are being performed by Django during app development lifecycle:

  • manage.py check
  • manage.py runserver
  • manage.py makemigrations
  • manage.py migrate
  • and so on...

This check is relied on the same mechanics that you might see in plain JSONField, complaining on passing mutable structures itself, instead of callables (Django's field.E010 warning).

Additional integrity checks

Along with the schema evaluation check, the field now performes a few others, to make sure of its integrity:

  • pydantic.E001 (a check from the section above). The schema cannot be resolved. It is most likely a programmatic error -- forward references cannot be resolved in the Django model's execution context.
  • pydantic.E002. The field's default= value cannot be serialized by the specified schema. This may lead to runtime errors during model .save() or .objects.create(...) methods.
  • pydantic.E003. If the field contains include= or exclude= export arguments, there could be a situation that value, written in the database, could not be restored back to python from its serialized form. This check tries to pass the specified default value (or the one that could be inferred directly from the schema, if default= is missing), through the whole transformation cycle, yielding a warning if the value could not be transformed back to python.

Additionally, JSONField's field.E010 warning is suppressed, as it is meaningless due to the nature of field transformations -- we're always getting a new value, not reusing the one passed to default= argument.

What's Changed

New Contributors

Full Changelog: https://github.com/surenkov/django-pydantic-field/compare/v0.2.14...v0.3.0

django-pydantic-field - v0.3.0b2

Published by surenkov 9 months ago

What's Changed

New Contributors

Full Changelog: https://github.com/surenkov/django-pydantic-field/compare/v0.3.0b1...v0.3.0b2

django-pydantic-field - v0.2.14

Published by surenkov 9 months ago

What's Changed

Full Changelog: https://github.com/surenkov/django-pydantic-field/compare/v0.2.13...v0.2.14

django-pydantic-field - v0.3.0b1

Published by surenkov 10 months ago

django-pydantic-field - v0.3.0a5

Published by surenkov 10 months ago

What's Changed

Full Changelog: https://github.com/surenkov/django-pydantic-field/compare/v0.3.0a4...v0.3.0a5

django-pydantic-field - v0.2.13

Published by surenkov 10 months ago

django-pydantic-field - v0.3.0a4

Published by surenkov 10 months ago

What's Changed

Full Changelog: https://github.com/surenkov/django-pydantic-field/compare/v0.3.0a3...v0.3.0a4

django-pydantic-field - v0.3.0a3: Django 5.0 support with Pydantic 2 🥳

Published by surenkov 11 months ago

django-pydantic-field - v0.2.12: Django 5.0 support

Published by surenkov 11 months ago

Package Rankings
Top 11.13% on Pypi.org
Badges
Extracted from project README
PyPI Version Lint and Test Package PyPI - Downloads Supported Python Versions Supported Django Versions