Opened 6 years ago

Closed 5 years ago

Last modified 5 years ago

#30557 closed Bug (fixed)

order_by() a parent model crash when Meta.ordering contains expressions.

Reported by: Jonny Fuller Owned by: Hasan Ramezani
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords: ordering
Cc: Hasan Ramezani Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Jonny Fuller)

Hi friends,

During testing I discovered a strange bug when using a query expression for ordering during multi-table inheritance. You can find the full write up as well as reproducible test repository https://github.com/JonnyWaffles/djangoordermetabug. The bug occurs because the field is an OrderBy object, not a string, during get_order_dir. The linked stacktrace should make the issue obvious, but what I don't understand is why it only fails during test db setup, not during repl or script use. I wish I could help more and come up with a real solution. Hopefully, this is enough for someone wiser to find the culprit.

Attachments (1)

30557.diff (1.9 KB ) - added by Mariusz Felisiak 6 years ago.
Regression test.

Download all attachments as: .zip

Change History (15)

comment:1 by Jonny Fuller, 6 years ago

Description: modified (diff)

comment:2 by Mariusz Felisiak, 6 years ago

Summary: Using Query Expression for ordering in multi-table inheritance scenario triggers TypeError during testorder_by() by a parent model crash in multi-table inheritance when Meta.ordering contains expressions.
Triage Stage: UnreviewedAccepted
Version: 2.2master

Thanks for the report. I attached a regression test.

Reproduced at c498f088c584ec3aff97409fdc11b39b28240de9.

by Mariusz Felisiak, 6 years ago

Attachment: 30557.diff added

Regression test.

comment:3 by Mariusz Felisiak, 6 years ago

Summary: order_by() by a parent model crash in multi-table inheritance when Meta.ordering contains expressions.order_by() a parent model crash when Meta.ordering contains expressions.

comment:4 by Alex Scott, 6 years ago

I *think* I'm getting a similar (or same) error when adding lowercased ordering to a model via meta:

class Recipe(models.Model):
    # ...
    class Meta:
        ordering = (Lower('name'),)

This works fine in normal views (listing out recipes for instance), but in the admin, I get the following error:
'Lower' object is not subscriptable

which also comes via get_order_dir()

comment:5 by Mariusz Felisiak, 6 years ago

Yes that's the same issue, thanks for another scenario to reproduce it.

comment:6 by Eran Keydar, 6 years ago

Owner: changed from nobody to Eran Keydar
Status: newassigned

comment:7 by Eran Keydar, 6 years ago

Has patch: set

comment:9 by Mariusz Felisiak, 6 years ago

Patch needs improvement: set

comment:10 by Mariusz Felisiak, 5 years ago

Cc: Hasan Ramezani added

comment:11 by Eran Keydar, 5 years ago

Owner: Eran Keydar removed
Status: assignednew

comment:12 by Mariusz Felisiak, 5 years ago

Owner: set to Hasan Ramezani
Patch needs improvement: unset
Status: newassigned

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

Resolution: fixed
Status: assignedclosed

In 8c5f9906:

Fixed #30557 -- Fixed crash of ordering by ptr fields when Meta.ordering contains expressions.

comment:14 by GitHub <noreply@…>, 5 years ago

In 7a42cfcf:

Refs #30557 -- Fixed crash of ordering by ptr fields when Meta.ordering contains F() expressions.

Thanks Can Sarıgöl for the report.

Follow up to 8c5f9906c56ac72fc4f13218dd90bdf9bc8a248b.

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