﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
34397	Subclasses of JSONField call `get_prep_value` with field as value	pheki	nobody	"Hi!

Let's say I have a class called PhoneNumber:


{{{
@dataclass
class PhoneNumber:
    country_iso_code: str
    national_number: int
}}}

I create a field I use to use PhoneNumber in my database:


{{{
class PhoneNumberField(JSONField):
    def get_prep_value(
        self, value: Optional[PhoneNumber]
    ) -> Optional[JsonAdapter]:
        if isinstance(value, PhoneNumber):
            return JsonAdapter(dataclasses.asdict(value), encoder=CustomJSONEncoder)
        elif value is None and self.null:
            return None
        else:
            raise Exception(""Fetching phone number with unexpected value"")

    # Similar from_db_value ommited
    def from_db_value(...):
        ...
}}}

The problem is that when I do a KeyTextTransform to get a field, it will call get_prep_value with that field and AFAIK there's no way to know which field is it getting. For from_db_value the same happens, but it's fine as you can inspect the expression column. Example:


{{{
User.objects.annotate(
    national_number=KeyTextTransform(""national_number"", ""phone_number"")
).filter(
    national_number=""12345678""
)[0]
}}}

Will call `field.get_prep_value(""12345678"")`, forcing the only way to make it work correctly to silently ignore unexpected types. This didn't happen on django 2.2 for `django.contrib.postgres.fields.jsonb.JSONField`."	Bug	closed	Database layer (models, ORM)	3.2	Normal	invalid			Unreviewed	0	0	0	0	0	0
