#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: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Julie Rymer:
Old description:
> 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.
>
> 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.
> 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.
>
> PS: #34397 seems to be related but in fact is about Django 3.2 so it is
> not the current issue
New description:
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.
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.
PS: #34397 seems to be related but in fact is about Django 3.2 so it is
not the current issue
--
--
Ticket URL: <https://code.djangoproject.com/ticket/34539#comment:1>
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/01070187e623ada8-4510aea7-ceac-46a2-931f-f24e133b4f3b-000000%40eu-central-1.amazonses.com.