Django ORM Cast() returning double quoted string from JSON field

I need to annotate a value that is saved in a json field in the same model. (Not the smartest but it is what it is).

I am annotating the value as such:

class SomeModel(BaseModel):
    reference_numbers = JSONField(blank=True, null=True)


I need it to be cast to text/char in the query because a subsequent search will only work on text/char (trigram similarity).

It works, sort of, but the result adds an extra quote to my string. Like so:

queryset[0].reference -> '"666999"'

Any ideas on how to get the correct string from the query?

I've also tried using just an ExpressionWrapper with the output field but since it doesnt cast the type in the SQL the code breaks afterwards when trying to execute the search because it uses the jsonb field still.

Back to Top