[Django] #34500: use annotate return feild, Error capture does not work

13 views
Skip to first unread message

Django

unread,
Apr 17, 2023, 3:47:14 AM4/17/23
to django-...@googlegroups.com
#34500: use annotate return feild, Error capture does not work
-------------------------------------+-------------------------------------
Reporter: ajinwu | Owner: nobody
Type: Bug | Status: new
Component: Database | Version: 4.2
layer (models, ORM) |
Severity: Normal | Keywords: json decode
Triage Stage: | Has patch: 1
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
-------------------------------------+-------------------------------------
when I use this sql
return data

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.

Django

unread,
Apr 17, 2023, 3:47:56 AM4/17/23
to django-...@googlegroups.com
#34500: use annotate return feild, Error capture does not work
-------------------------------------+-------------------------------------
Reporter: ajinwu | Owner: ajinwu
Type: Bug | Status: assigned
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution:

Keywords: json decode | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by ajinwu):

* owner: nobody => ajinwu
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/34500#comment:1>

Django

unread,
Apr 17, 2023, 4:01:45 AM4/17/23
to django-...@googlegroups.com
#34500: use annotate return feild, Error capture does not work
-------------------------------------+-------------------------------------
Reporter: ajinwu | Owner: ajinwu
Type: Bug | Status: closed

Component: Database layer | Version: 4.2
(models, ORM) | Resolution:
Severity: Normal | worksforme

Keywords: json decode | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* 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>

Django

unread,
Apr 17, 2023, 4:06:25 AM4/17/23
to django-...@googlegroups.com
#34500: use annotate return feild, Error capture does not work
-------------------------------------+-------------------------------------
Reporter: ajinwu | Owner: ajinwu
Type: Bug | Status: closed
Component: Database layer | Version: 4.2
(models, ORM) | Resolution:
Severity: Normal | worksforme
Keywords: json decode | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Apr 17, 2023, 4:08:10 AM4/17/23
to django-...@googlegroups.com
#34500: use annotate return feild, Error capture does not work
-------------------------------------+-------------------------------------
Reporter: ajinwu | Owner: ajinwu
Type: Bug | Status: closed
Component: Database layer | Version: 4.2
(models, ORM) | Resolution:
Severity: Normal | worksforme
Keywords: json decode | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Apr 17, 2023, 4:10:08 AM4/17/23
to django-...@googlegroups.com
#34500: use annotate return feild, Error capture does not work
-------------------------------------+-------------------------------------
Reporter: ajinwu | Owner: ajinwu
Type: Bug | Status: new

Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution:

Keywords: json decode | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by ajinwu):

* status: closed => new
* resolution: worksforme =>


--
Ticket URL: <https://code.djangoproject.com/ticket/34500#comment:5>

Django

unread,
Apr 17, 2023, 4:15:05 AM4/17/23
to django-...@googlegroups.com
#34500: use annotate return feild, Error capture does not work
-------------------------------------+-------------------------------------
Reporter: ajinwu | Owner: ajinwu
Type: Bug | Status: closed

Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution: duplicate

Keywords: json decode | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* 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>

Reply all
Reply to author
Forward
0 new messages