#34539: `get_prep_value` no longer called for JSONField
-------------------------------------+-------------------------------------
Reporter: Julie Rymer | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Mariusz Felisiak):
Thanks for report. The way how built-in fields are implemented is an
implementation detail and can change without the deprecation path.
Replying to [ticket:34539 Julie Rymer]:
> Hello, I just upgraded from django 4.1 to 4.2 and I have a custom
`JSONField` with a `get_prep_value()` override that stopped working.
After searching a bit, I saw that was because `JSONField.get_prep_value()`
is no longer called in 4.2
([https://github.com/django/django/commit/5c23d9f0c32f166c81ecb6f3f01d5077a6084318
5c23d9f0c32f166c81ecb6f3f01d5077a6084318]).
>
> I think this issue need a resolution either:
>
> - `JSONField` should call `get_prep_value()` like all other fields type,
because this is the method that the
[https://docs.djangoproject.com/en/4.2/ref/models/fields/#django.db.models.Field
documentation] tell us to override in custom fields.
Both methods `get_prep_value()` and `get_db_prep_value()` are documented
there. I don't see any preferences for `get_prep_value()` 🤔.
> Otherwise we need to override `get_db_prep_value()` which is heavier and
does not have the same purpose. I think simply replacing
`connection.ops.adapt_json_value(value, self.encoder)` with
`connection.ops.adapt_json_value(self.get_prep_value(value),
self.encoder)` in `JSONField.get_db_prep_value()` would fix this
>
> - If there is a good reason to no longer call `get_prep_value()`, this
exception for `JSONField` should be clearly documented in
[https://docs.djangoproject.com/en/4.2/ref/models/fields/#django.db.models.Field.get_prep_value
custom get_prep_value() doc]. It should also be added to
[https://docs.djangoproject.com/en/4.2/releases/4.2/#backwards-
incompatible-changes-in-4-2 Backwards incompatible changes in 4.2 release
note] because I got stuck with this issue with no warning when migrating.
This is not really an exception, there are many built-in fields that don't
call `get_prep_value()` in `get_db_prep_value()`, e.g. `ArrayField` or
`DurationField`.
You can check how, for example, Wagtail solved
[https://github.com/wagtail/wagtail/issues/9692 the same issue] on their
side.
--
Ticket URL: <https://code.djangoproject.com/ticket/34539#comment:3>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/01070187e63ab582-1b40095c-e842-4b1f-a194-da52da291a81-000000%40eu-central-1.amazonses.com.