#30463: Deprecation message generates error when query expression is used in
Meta.ordering
-------------------------------------+-------------------------------------
               Reporter:  Jannis     |          Owner:  nobody
                   Type:             |         Status:  new
  Uncategorized                      |
              Component:  Database   |        Version:  2.2
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:  ordering
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  1
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 Since updating to Django 2.2 our test suite fails because the newly
 introduced deprecation warning which warns about Meta.ordering being
 ignored from Django 3.1 onwards leads to errors when a query expression is
 used.

 Take a model definition like this as an example:


 {{{
 class Book
     name = models.CharField(max_length=255)

     class Meta:
         ordering = [F('name',).asc()]

 }}}

 The error happens here:

 {{{
 File "django/django/db/models/sql/compiler.py", line 558, in as_sql
     "', '".join(self._meta_ordering)
 TypeError: sequence item 0: expected str instance, OrderBy found
 }}}

 A quick and dirty way around that problem is to join the string
 representations of all the list items instead of concatenating them
 directly:

 {{{
     warnings.warn(
         "%s QuerySet won't use Meta.ordering in Django 3.1. "
         "Add .order_by('%s') to retain the current query." % (
             self.query.model.__name__,
             "', '".join([str(f) for f in self._meta_ordering])
         ),
     )
 }}}

 Unfortunately this doesn't generate real source code compatible with
 `.order_by()` because the quotation marks are not correct.
 Maybe someone else has a clean solution on how to fix this?

 {{{
 - Book QuerySet won't use Meta.ordering in Django 3.1. Add
 .order_by('name', 'OrderBy(F(price), descending=False)') to retain the
 current query.
 -                                   -
 + Book QuerySet won't use Meta.ordering in Django 3.1. Add
 .order_by('name', OrderBy(F('price'), descending=False)) to retain the
 current query.
 }}}


 A regression test is available here: https://github.com/jnns/django/tree
 /meta-ordering-deprecation-warning

-- 
Ticket URL: <https://code.djangoproject.com/ticket/30463>
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 django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/047.054645fc694fbe37cdfdaf8ce0c7d121%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to