Opened 5 months ago

Closed 5 months ago

Last modified 5 months ago

#30463 closed Bug (fixed)

Deprecation message crashes when using a query expression in Model.ordering.

Reported by: Jannis Owned by: Ruchit Vithani
Component: Database layer (models, ORM) Version: 2.2
Severity: Release blocker Keywords: ordering
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

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

Attachments (1)

ticket-30463.diff (448 bytes) - added by felixxm 5 months ago.
Regression test.

Download all attachments as: .zip

Change History (11)

comment:1 Changed 5 months ago by felixxm

Severity: NormalRelease blocker
Summary: Deprecation message generates error when query expression is used in Meta.orderingDeprecation message crashes when using a query expression in Model.ordering.
Type: UncategorizedBug

Thanks for the report!

Reproduced at ef9f2eb69c9396683cefa742bc7d0a0792090e8d.
Regression in 1b1f64ee5a78cc217fead52cbae23114502cf564.

Changed 5 months ago by felixxm

Attachment: ticket-30463.diff added

Regression test.

comment:2 Changed 5 months ago by Carlton Gibson

Triage Stage: UnreviewedAccepted

comment:3 Changed 5 months ago by Ruchit Vithani

Owner: changed from nobody to Ruchit Vithani
Status: newassigned

comment:4 in reply to:  2 ; Changed 5 months ago by Ruchit Vithani

Replying to Carlton Gibson: Is it alright if I assign this ticket to me?

comment:5 in reply to:  4 Changed 5 months ago by Simon Charette

Replying to Ruchit Vithani: Feel free to assign the ticket to yourself if you plan on working on it.

comment:6 Changed 5 months ago by Ruchit Vithani

Has patch: set
Patch needs improvement: set

comment:7 Changed 5 months ago by Ruchit Vithani

comment:8 Changed 5 months ago by felixxm

Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

comment:9 Changed 5 months ago by Mariusz Felisiak <felisiak.mariusz@…>

Resolution: fixed
Status: assignedclosed

In 04042b2b:

Fixed #30463 -- Fixed crash of deprecation message when Meta.ordering contains expressions.

Regression in 1b1f64ee5a78cc217fead52cbae23114502cf564.

comment:10 Changed 5 months ago by Mariusz Felisiak <felisiak.mariusz@…>

In db7d7901:

[2.2.x] Fixed #30463 -- Fixed crash of deprecation message when Meta.ordering contains expressions.

Regression in 1b1f64ee5a78cc217fead52cbae23114502cf564.

Backport of 04042b2b440f0bf50eb908d52cfe76af430e1738 from master

Note: See TracTickets for help on using tickets.
Back to Top