#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 Julie Rymer):
Replying to [comment:3 Mariusz Felisiak]:
> Thanks for report. The way how built-in fields are implemented is an
implementation detail and can change without the deprecation path.
>
> ...
>
> Both methods `get_prep_value()` and `get_db_prep_value()` are documented
there. I don't see any preferences for `get_prep_value()` 🤔.
It doesn't seems like an implementation detail when those function are
clearly documented for customisation.
As for the preference, the first paragraph of the
[https://docs.djangoproject.com/en/4.2/ref/models/fields/#django.db.models.Field
Field API doc] is
> Field is an abstract class that represents a database table column.
Django uses fields to create the database table (db_type()), to map Python
types to database (get_prep_value()) and vice-versa (from_db_value()).
`get_prep_value` is the documented method for mapping Python types to
database, which is what I was trying to do, `get_db_prep_value` is only
for backend specific need so not the most common case (hence why only
`get_prep_value` is put foremost in the intro I suppose).
Replying to [comment:4 Mariusz Felisiak]:
> Also, the docs that you mentioned is Field API reference not How to
create a custom field or How to override build-in fields, so it only
describes methods available in the Field API.
You are right, however the [https://docs.djangoproject.com/en/4.2/howto
/custom-model-fields/#converting-python-objects-to-query-values
customisation doc] still does not mention that `get_prep_value()` is not
always available for override.
Replying to [comment:3 Mariusz Felisiak]:
> 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.
Then it should be changed, or documented, for those fields also :)
Like wagtail, I also ended up overriding `get_db_prep_value()` instead of
`get_prep_value()`, but this seems like too much work when I don't care
about the backend and simply want to serialise my custom object.
--
Ticket URL: <https://code.djangoproject.com/ticket/34539#comment:6>
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/01070187e64c9cca-277de111-8efc-4fd4-835e-95e76af41e06-000000%40eu-central-1.amazonses.com.