#34013: order_by queryset does not work with annotated functions.JSONObject 
fields
-------------------------------------+-------------------------------------
               Reporter:  akamoroz   |          Owner:  nobody
                   Type:  Bug        |         Status:  new
              Component:  Database   |        Version:  dev
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:  orm, json, ordering
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 Please see the example:

 {{{
 #!python
 queryset = Model.objects.all().annotate(
     json_field=functions.JSONObject(
         test_pk=models.Value(100) + models.F("pk"),
     ),
     test_pk_from_json_field=models.F("json_field__test_pk"),
 )
 print(queryset.order_by("json_field__test_pk"))  # AttributeError:
 'JSONField' object has no attribute 'model'
 print(queryset.order_by("test_pk_from_json_field"))  # Successful
 }}}

 Full traceback for ordering by `json_field__test_pk`:
 {{{
 Traceback (most recent call last):
   File "<console>", line 1, in <module>
   File "/usr/local/lib/python3.10/site-
 packages/django/db/models/query.py", line 256, in __repr__
     data = list(self[:REPR_OUTPUT_SIZE + 1])
   File "/usr/local/lib/python3.10/site-
 packages/django/db/models/query.py", line 280, in __iter__
     self._fetch_all()
   File "/usr/local/lib/python3.10/site-
 packages/django/db/models/query.py", line 1324, in _fetch_all
     self._result_cache = list(self._iterable_class(self))
   File "/usr/local/lib/python3.10/site-
 packages/django/db/models/query.py", line 51, in __iter__
     results = compiler.execute_sql(chunked_fetch=self.chunked_fetch,
 chunk_size=self.chunk_size)
   File "/usr/local/lib/python3.10/site-
 packages/django/db/models/sql/compiler.py", line 1162, in execute_sql
     sql, params = self.as_sql()
   File "/usr/local/lib/python3.10/site-
 packages/django/db/models/sql/compiler.py", line 513, in as_sql
     extra_select, order_by, group_by = self.pre_sql_setup()
   File "/usr/local/lib/python3.10/site-
 packages/django/db/models/sql/compiler.py", line 56, in pre_sql_setup
     order_by = self.get_order_by()
   File "/usr/local/lib/python3.10/site-
 packages/django/db/models/sql/compiler.py", line 356, in get_order_by
     order_by.extend(self.find_ordering_name(
   File "/usr/local/lib/python3.10/site-
 packages/django/db/models/sql/compiler.py", line 769, in
 find_ordering_name
     return [(OrderBy(transform_function(t, alias), descending=descending),
 False) for t in targets]
   File "/usr/local/lib/python3.10/site-
 packages/django/db/models/sql/compiler.py", line 769, in <listcomp>
     return [(OrderBy(transform_function(t, alias), descending=descending),
 False) for t in targets]
   File "/usr/local/lib/python3.10/site-
 packages/django/db/models/sql/query.py", line 1666, in transform
     wrapped = previous(field, alias)
   File "/usr/local/lib/python3.10/site-
 packages/django/db/models/sql/query.py", line 1641, in final_transformer
     return field.get_col(alias)
   File "/usr/local/lib/python3.10/site-
 packages/django/db/models/fields/__init__.py", line 397, in get_col
     if alias != self.model._meta.db_table or output_field != self:
 AttributeError: 'JSONField' object has no attribute 'model'
 }}}

 It seems there is a lack of support JSONField in order_by queryset.

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

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018341cafc02-9e9d3b39-ff8c-41e4-93ad-cde67a7a03d6-000000%40eu-central-1.amazonses.com.

Reply via email to