# Model fields: calling \`get\_prep\_value\` from \`get\_db\_prep\_value\`

**URL:** <https://forum.djangoproject.com/t/model-fields-calling-get-prep-value-from-get-db-prep-value/20947>\
**Category:** ORM\
**Created:** [May 15, 2023, 5:07pm UTC](https://forum.djangoproject.com/t/model-fields-calling-get-prep-value-from-get-db-prep-value/20947 "2023-05-15T17:07:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nessita](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/nessita/32/12194_2.png) [@nessita](https://forum.djangoproject.com/u/nessita)\
**Post date:** [May 15, 2023, 5:07pm UTC](https://forum.djangoproject.com/t/model-fields-calling-get-prep-value-from-get-db-prep-value/20947/1 "2023-05-15T17:07:26Z")

</div>

Hello everyone,

I’ve been reviewing the [PR](https://github.com/django/django/pull/16858) for [this ticket](https://code.djangoproject.com/ticket/34539). This is about how `JSONField` is not calling `get_prep_value` when adapting the field content in `get_db_prep_value`.

I’ve been doing some reading of `django/db/models/fields/*.py` and I’ve noticed a few things, that I wanted to discuss with all of you to gain a better understanding of whether any of these items are worth creating tickets for and if they require improvement work to enhance consistency and predictability.

Considering the fact that the base class `Field` provides a `get_db_prep_value` implementation as follows:

```python
    def get_db_prep_value(self, value, connection, prepared=False):
        if not prepared:
            value = self.get_prep_value(value)
        return value

```

And that:

- There are many `Field` children that override `get_db_prep_value`, without calling `super()`, but repeating the same lines explicitly:

```python
    def get_db_prep_value(self, value, connection, prepared=False):
        if not prepared:
            value = self.get_prep_value(value)
        # do something else with value...
        return modified_value

```

- `IntegerField` and `BinaryField` do call `super()` in `get_db_prep_value` 💪
- A small subset of fields would not call `get_prep_value` in `get_db_prep_value` at all. These are `DurationField`, `UUIDField`.

Would it make sense to have those `Field` children preparing the value “by hand” calling `super()` instead? Shall we also call `get_prep_value` (one way or another) in the two fields that do not currently call it? (`JSONField` is already being fixed so I’m not counting it here).

---

<div class="post-metadata">

**Author:** ![charettes](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/charettes/32/27_2.png) [@charettes](https://forum.djangoproject.com/u/charettes)\
**Post date:** [May 16, 2023, 6:09pm UTC](https://forum.djangoproject.com/t/model-fields-calling-get-prep-value-from-get-db-prep-value/20947/2 "2023-05-16T18:09:25Z")

</div>

I’m not opposed to making changes that make the usage of `get_prep_value` and `get_db_prep_value` more coherent in the core fields that Django. Given how often some of them are used for reference implementation it seems like a worthy change.

---

<div class="post-metadata">

**Author:** ![Chadys](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/chadys/32/13764_2.png) [@Chadys](https://forum.djangoproject.com/u/Chadys)\
**Post date:** [May 17, 2023, 8:52am UTC](https://forum.djangoproject.com/t/model-fields-calling-get-prep-value-from-get-db-prep-value/20947/3 "2023-05-17T08:52:28Z")

</div>

I opened a [new ticket](https://code.djangoproject.com/ticket/34569#ticket) about the same subject before seeing your thread. There are some more fields listed there besides `DurationField` and `UUIDField`.
