Skip to content

Commit

Permalink
Merge pull request #78 from surenkov/feature/output-field-annotations…
Browse files Browse the repository at this point in the history
…-casting

Type adaptation in `from_db_value`
  • Loading branch information
surenkov authored Jan 14, 2025
2 parents 48d42bc + 518966e commit 22a82e7
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions django_pydantic_field/v2/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from django.core.serializers.json import DjangoJSONEncoder
from django.db.models.expressions import BaseExpression, Col, Value
from django.db.models.fields import NOT_PROVIDED
from django.db.models.fields.json import JSONField
from django.db.models.fields.json import JSONField, KeyTransform
from django.db.models.lookups import Transform
from django.db.models.query_utils import DeferredAttribute

Expand Down Expand Up @@ -172,6 +172,18 @@ def to_python(self, value: ty.Any):
except pydantic.ValidationError as exc:
raise exceptions.ValidationError(str(exc), code="invalid") from exc

def from_db_value(self, value, expression, connection):
if value is None:
return value
# Some backends (SQLite at least) extract non-string values in their SQL datatypes.
if isinstance(expression, KeyTransform) and not isinstance(value, str):
return value

try:
return self.adapter.validate_json(value)
except ValueError:
return value

def get_prep_value(self, value: ty.Any):
value = self._prepare_raw_value(value)
return super().get_prep_value(value)
Expand All @@ -188,9 +200,10 @@ def get_default(self) -> ty.Any:
return self.adapter.validate_python(default_value)
return default_value

def formfield(self, **kwargs):
def formfield(self, form_class=None, choices_form_class=None, **kwargs):
field_kwargs = dict(
form_class=forms.SchemaField,
form_class=form_class or forms.SchemaField,
choices_form_class=choices_form_class,
# Trying to resolve the schema before passing it to the formfield, since in Django < 4.0,
# formfield is unbound during form validation and is not able to resolve forward refs defined in the model.
schema=self.adapter.prepared_schema,
Expand Down

0 comments on commit 22a82e7

Please sign in to comment.