Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#30463 closed Bug (fixed)

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

Reported by: Jannis Vajen 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 Mariusz Felisiak 5 years ago.
Regression test.

Download all attachments as: .zip

Change History (11)

comment:1 by Mariusz Felisiak, 5 years ago

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.

by Mariusz Felisiak, 5 years ago

Attachment: ticket-30463.diff added

Regression test.

comment:2 by Carlton Gibson, 5 years ago

Triage Stage: UnreviewedAccepted

comment:3 by Ruchit Vithani, 5 years ago

Owner: changed from nobody to Ruchit Vithani
Status: newassigned

in reply to:  2 ; comment:4 by Ruchit Vithani, 5 years ago

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

in reply to:  4 comment:5 by Simon Charette, 5 years ago

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

comment:6 by Ruchit Vithani, 5 years ago

Has patch: set
Patch needs improvement: set

comment:8 by Mariusz Felisiak, 5 years ago

Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

comment:9 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

Resolution: fixed
Status: assignedclosed

In 04042b2b:

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

Regression in 1b1f64ee5a78cc217fead52cbae23114502cf564.

comment:10 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

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