testcase
{{{
#!div style="font-size: 80%"
{{{#!python
data = table.objects.annotate(
array_length=Func(F('feild'), function='jsonb_array_length')
).filter(array_length__gt=1).only("feild")[:5]
print(data.query)
for i in data:
print(model_to_dict(i))
}}}
}}}
sqldata
{{{
#!div style="font-size: 80%"
{{{#!python
data = [[1, '[{"key": "value"}]', 1]]
}}}
}}}
but when feild use from_db_value function, Error capture has no effect,
because json load int data, and can not return value
{{{
#!div style="font-size: 80%"
{{{#!python
try:
return json.loads(value, cls=self.decoder)
except json.JSONDecodeError:
return value
}}}
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/34500>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* owner: nobody => ajinwu
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/34500#comment:1>
* status: assigned => closed
* has_patch: 1 => 0
* resolution: => worksforme
Comment:
Thanks for the report, however I really don't understand what kind of
issue you're trying to report. Using `model_to_dict()` on models with
`JSONField()` works for me. Please try to be more descriptive and provide
a sample project or a reproducible scenario.
--
Ticket URL: <https://code.djangoproject.com/ticket/34500#comment:2>
Comment (by ajinwu):
model_to_dict is not import,That's a recurring example , this code can not
return value, if value is int, that can not return true data
{{{
#!div style="font-size: 80%"
{{{#!python
try:
return json.loads(value, cls=self.decoder)
except json.JSONDecodeError:
return value
}}}
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/34500#comment:3>
Comment (by ajinwu):
that can not print return
{{{
#!div style="font-size: 80%"
{{{#!python
import json
try:
json.loads(3)
except json.JSONDecodeError:
print("return")
}}}
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/34500#comment:4>
* status: closed => new
* resolution: worksforme =>
--
Ticket URL: <https://code.djangoproject.com/ticket/34500#comment:5>
* status: new => closed
* resolution: => duplicate
Comment:
`JSONField.from_db_value()` is a part of Django internals and it works as
expected. `value` returned from a database should be a string. If you want
to use `Func()` which returns a type other than the field you passed in,
you should use the `output_field` argument, e.g.
{{{
Func(F('field'), function='jsonb_array_length',
output_field=IntegerField())
}}}
Duplicate of #31973.
--
Ticket URL: <https://code.djangoproject.com/ticket/34500#comment:6>