Opened 6 weeks ago

Closed 10 days ago

Last modified 10 days 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: master
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 felixxm 6 weeks ago.
Regression test.

Download all attachments as: .zip

Change History (15)

comment:1 Changed 6 weeks ago by Jonny Fuller

Description: modified (diff)

comment:2 Changed 6 weeks ago by felixxm

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.

Changed 6 weeks ago by felixxm

Attachment: 30557.diff added

Regression test.

comment:3 Changed 6 weeks ago by felixxm

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 Changed 6 weeks ago by Alex Scott

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 Changed 6 weeks ago by felixxm

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

comment:6 Changed 5 weeks ago by Eran Keydar

Owner: changed from nobody to Eran Keydar
Status: newassigned

comment:7 Changed 5 weeks ago by Eran Keydar

Has patch: set

comment:9 Changed 5 weeks ago by felixxm

Patch needs improvement: set

comment:10 Changed 11 days ago by felixxm

Cc: Hasan Ramezani added

comment:11 Changed 11 days ago by Eran Keydar

Owner: Eran Keydar deleted
Status: assignednew

comment:12 Changed 11 days ago by felixxm

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

comment:13 Changed 10 days ago by Mariusz Felisiak <felisiak.mariusz@…>

Resolution: fixed
Status: assignedclosed

In 8c5f9906:

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

comment:14 Changed 10 days ago by GitHub <noreply@…>

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