Skip to content

Releases: surenkov/django-pydantic-field

v0.3.0a3: Django 5.0 support with Pydantic 2 🥳

04 Dec 22:19
Compare
Choose a tag to compare

What's Changed

Full Changelog: v0.3.0a2...v0.3.0a3

v0.2.12: Django 5.0 support

04 Dec 14:12
Compare
Choose a tag to compare

What's Changed

Full Changelog: v0.2.11...v0.2.12

v0.3.0a2: Tweaks on package discovery for Pydantic v2

28 Nov 18:50
Compare
Choose a tag to compare

Previous pre-release contained a broken package. Here's the new one (:

Full Changelog: v0.3.0a1...v0.3.0a2

v0.3.0a1: Pydantic v2 initial support

28 Nov 18:03
Compare
Choose a tag to compare
Pre-release

Pydantic v2 initial support

Important note: 0.3.0a1 contained a broken package. Please take a look on 0.3.0a2 if you like to test it in the wild (:

This release is specifically focused on adding v2 primitives. Pydantic v1 was supported by a trickery around passed (or annotated) schema, wrapping it into intermediary RootModel to support both pydantic models and most of arbitrary annotations.

With v2, pydantic now contains the primitive -- TypeAdapter -- which is well designed for this particular machinery.

There's one remaining primitive is to be migrated prior final 0.3.0 -- OpenAPI AutoSchema generator, but since this library is already not super-sticky to the OpenAPI standard, I decided that alpha release could be rolled out without it.

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.11.

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.

Full Changelog: v0.2.11...v0.3.0a1

v0.2.11

18 Oct 23:31
Compare
Choose a tag to compare

What's Changed

New Contributors

Full Changelog: v0.2.10...v0.2.11

v0.2.10

27 Aug 17:24
Compare
Choose a tag to compare
v0.2.10 Pre-release
Pre-release

What's Changed

  • Raise ValidationError on form invalid JSON payload by @surenkov in #29

Full Changelog: v0.2.9...v0.2.10

v0.2.9

24 Aug 07:25
Compare
Choose a tag to compare
v0.2.9 Pre-release
Pre-release

What's Changed

  • Ensure that attributes set during contribute_to_class are copied. by @TomTruck in #27

New Contributors

Full Changelog: v0.2.8...v0.2.9

v0.2.8

24 Jul 08:14
618c439
Compare
Choose a tag to compare
v0.2.8 Pre-release
Pre-release

What's Changed

  • Add backward support for pydantic 1.9.* by @surenkov in #26

Full Changelog: v0.2.7...v0.2.8

v0.2.7

19 Jun 17:43
Compare
Choose a tag to compare
v0.2.7 Pre-release
Pre-release

What's Changed

  • Updates to deconstruct() in SchemaField to preserve values of include and exclude by @oomojola in #23

New Contributors

Full Changelog: v0.2.6...v0.2.7

v0.2.6

16 Jun 13:07
bd8f106
Compare
Choose a tag to compare
v0.2.6 Pre-release
Pre-release

What's Changed

  • Bring back JSON field lookups to work with schema [Django 4.2] by @surenkov in #22

Full Changelog: v0.2.5...v0.2.6