Re: [Django] #34013: order_by() crashes with annotated JSONObject fields.

23 views
Skip to first unread message

Django

unread,
Sep 16, 2022, 7:46:19 AM9/16/22
to django-...@googlegroups.com
#34013: order_by() crashes with annotated JSONObject fields.
-------------------------------------+-------------------------------------
Reporter: Eugene Morozov | Owner: Aman
| Pandey
Type: Bug | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: orm, json, ordering | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Aman Pandey):

* owner: nobody => Aman Pandey
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/34013#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Sep 16, 2022, 3:21:59 PM9/16/22
to django-...@googlegroups.com
#34013: order_by() crashes with annotated JSONObject fields.
-------------------------------------+-------------------------------------
Reporter: Eugene Morozov | Owner: Aman
| Pandey
Type: Bug | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: orm, json, ordering | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Aman Pandey):

"It seems there is a lack of support JSONField in order_by queryset."
this is not true as JSONField based example queryset is working fine with
order_by method
eg:
""""
class Dog(models.Model):
name = models.CharField(max_length=200)
data = models.JSONField(null=True)

def __str__(self):
return self.name
""""

>>> dogs = Dog.objects.all().order_by("data")
>>> dogs
<QuerySet [<Dog: stella>, <Dog: tuffy>, <Dog: shifu>, <Dog: milo>]>

this is working ..
can someone please tell me about this model attribute how does this
assigned to the object
or wheather i should start with annotate class?

--
Ticket URL: <https://code.djangoproject.com/ticket/34013#comment:3>

Django

unread,
Sep 18, 2022, 12:11:04 PM9/18/22
to django-...@googlegroups.com
#34013: order_by() crashes with annotated JSONObject fields.
-------------------------------------+-------------------------------------
Reporter: Eugene Morozov | Owner: Aman
| Pandey
Type: Bug | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: orm, json, ordering | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Aman Pandey):

i think the issue is JSONObject returns a JSONField and since it's
annotated JSONField doesn't get populated with the details of model it
belongs to
since field generally are called in a model so they get populated with
details of that model , here JSONField didn't get any details , including
"self. model"
which causes an error .

but this is only happening with "___ " look up call in order_by , but not
with overall call , so i am little confused where to look forward to , to
fix it , JSONField, JSONObejct , order_by or Lookup ?

--
Ticket URL: <https://code.djangoproject.com/ticket/34013#comment:4>

Django

unread,
Sep 20, 2022, 2:11:29 PM9/20/22
to django-...@googlegroups.com
#34013: order_by() crashes with annotated JSONObject fields.
-------------------------------------+-------------------------------------
Reporter: Eugene Morozov | Owner: Aman
| Pandey
Type: Bug | Status: closed

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

Keywords: orm, json, ordering | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Aman Pandey):

* status: assigned => closed
* resolution: => fixed


Comment:

i was trying to populate the Model and freshly created JSONField with
each other's info like trying to populate JSONField.model with model info
and
model._meta with JSONField info while the execution of annotate->
order_by-> add_ordering ->names_to_path (where lookup is processed ) but

that approach is very naive and making me fall deeper.

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

Django

unread,
Sep 20, 2022, 2:11:42 PM9/20/22
to django-...@googlegroups.com
#34013: order_by() crashes with annotated JSONObject fields.
-------------------------------------+-------------------------------------
Reporter: Eugene Morozov | Owner: Aman
| Pandey
Type: Bug | Status: new

Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: orm, json, ordering | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Aman Pandey):

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


--
Ticket URL: <https://code.djangoproject.com/ticket/34013#comment:6>

Django

unread,
Dec 7, 2022, 8:18:38 AM12/7/22
to django-...@googlegroups.com
#34013: order_by() crashes with annotated JSONObject fields.
-------------------------------------+-------------------------------------
Reporter: Eugene Morozov | Owner: Aman
| Pandey
Type: Bug | Status: new
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: orm, json, ordering | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Rodolfo Valentín Becerra García):

Try this

{{{
Model.objects.all().annotate(
json_field=functions.JSONObject(
test_pk=models.Value(100) + models.F("pk"),
),
test_pk_from_json_field=KeyTextTransform(
"test_pk", "json_field"
)
)
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/34013#comment:7>

Django

unread,
Apr 30, 2023, 6:18:50 AM4/30/23
to django-...@googlegroups.com
#34013: order_by() crashes with annotated JSONObject fields.
-------------------------------------+-------------------------------------
Reporter: Eugene Morozov | Owner: Aman
| Pandey
Type: Bug | Status: new
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: orm, json, ordering | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by saadullahaleem):

Is this issue still valid? I'm trying to find a good first issue to create
a PR for and did some digging here.

It looks like a `model` is attached to a `Field` object using the
`contribute_to_class` method.

It seems that the `contribute_to_class` method is called when fields are
added to the model class during its creation or when fields are added
dynamically. In this case, we're not adding or modifying fields in the
model class; we're only working with the queryset and computing new values
based on existing fields. The queryset annotations do not modify the model
class itself, so `contribute_to_class` is not called in this context.

--
Ticket URL: <https://code.djangoproject.com/ticket/34013#comment:8>

Django

unread,
Apr 30, 2023, 7:15:12 AM4/30/23
to django-...@googlegroups.com
#34013: order_by() crashes with annotated JSONObject fields.
-------------------------------------+-------------------------------------
Reporter: Eugene Morozov | Owner: Aman
| Pandey
Type: Bug | Status: new
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: orm, json, ordering | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Eugene Morozov):

I haven't tested this problem with the latest versions of Django 4.2 and
postgres 3. Nonetheless, I don't believe that the issue is linked to the
modifications made in Django 4.2.

So, you can try to run the code from the example and I think it will be
still not working.

--
Ticket URL: <https://code.djangoproject.com/ticket/34013#comment:9>

Django

unread,
Apr 30, 2023, 7:32:06 AM4/30/23
to django-...@googlegroups.com
#34013: order_by() crashes with annotated JSONObject fields.
-------------------------------------+-------------------------------------
Reporter: Eugene Morozov | Owner: Aman
| Pandey
Type: Bug | Status: new
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: orm, json, ordering | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by saadullahaleem):

Replying to [comment:9 Eugene Morozov]:


> I haven't tested this problem with the latest versions of Django 4.2 and
postgres 3. Nonetheless, I don't believe that the issue is linked to the
modifications made in Django 4.2.
>
> So, you can try to run the code from the example and I think it will be
still not working.
>

Thanks for the quick response.

I was able to reproduce it on the main branch but to fix it, a model would
need to be attached to the JSONField inside the JSONObject method but it
can't be done since annotations don't modify the class itself. So it seems
like it might require a big architectural change for this to work.

--
Ticket URL: <https://code.djangoproject.com/ticket/34013#comment:10>

Reply all
Reply to author
Forward
0 new messages