﻿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
32079	Using JSONField with Func() crash when returns boolean.	Loic Quertenmont	nobody	"Hello,

I've jsut migrated to django 3 and I observed a bug when annotating a json field

I have a model with a json field named ""references"" which is basically a dictionnary,
If I annotate this field using a jsonb_path_exists function through a ""Func"" as shown bellow
and I then try to fetch or iterate over the results, it crashes in from_db_value (django/db/models/fields/json.py)
because it tries to decode the boolean output of this function via json.loads
{{{
           qs = qs.annotate(reference_match = Func(F('references'), ('$.* ? (%s)'%condition, ) , function='jsonb_path_exists')         
           print(list(qs)) # fails
}}}

changing the line to (adding the output_field of the function) is enough to make it work

{{{
           qs = qs.annotate(reference_match = Func(F('references'), ('$.* ? (%s)'%condition, ) , function='jsonb_path_exists',, output_field=models.BooleanField())         
           print(list(qs)) # success
}}}

This was not needed in django 2.2.X
Here it seems to believe that the Func has the same time has the referenced field.

Thanks in advance for fixing this.
Loic

PS: I am runnong on postgresql 12"	Bug	closed	Database layer (models, ORM)	3.1	Normal	invalid			Unreviewed	0	0	0	0	0	0
