#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)
Change History (11)
comment:1 by , 6 years ago
Severity: | Normal → Release blocker |
---|---|
Summary: | Deprecation message generates error when query expression is used in Meta.ordering → Deprecation message crashes when using a query expression in Model.ordering. |
Type: | Uncategorized → Bug |
follow-up: 4 comment:2 by , 6 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:3 by , 6 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
follow-up: 5 comment:4 by , 6 years ago
Replying to Carlton Gibson: Is it alright if I assign this ticket to me?
comment:5 by , 6 years ago
Replying to Ruchit Vithani: Feel free to assign the ticket to yourself if you plan on working on it.
comment:6 by , 6 years ago
Has patch: | set |
---|---|
Patch needs improvement: | set |
comment:8 by , 6 years ago
Patch needs improvement: | unset |
---|---|
Triage Stage: | Accepted → Ready for checkin |
Thanks for the report!
Reproduced at ef9f2eb69c9396683cefa742bc7d0a0792090e8d.
Regression in 1b1f64ee5a78cc217fead52cbae23114502cf564.