Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#28958 closed Bug (fixed)

Admin changelist crashes when using query expression in the model's Meta.ordering or ModelAdmin.ordering

Reported by: Gabriel Amram Owned by: felixxm
Component: contrib.admin Version: 2.0
Severity: Release blocker Keywords: query expresssion, ordering, admin, meta
Cc: felixxm 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 Gabriel Amram)

Using the newly introduced query-expression ordering feature in either the model's Meta.ordering or in its ModelAdmin causes errors.

Steps to reproduce:
First variant - using the model's Meta.ordering option -

//models.py
class Game(models.Model):
    score = models.IntegerField(null=True)

     class Meta:
         ordering = [F("score").desc(nulls_last=True)]
...
// admin.py
admin.site.register(Game)

Accessing the admin site will now give an AttributeError: 'OrderBy' object has no attribute 'startswith'

Variant 2 - Using the ModelAdmin ordering attribute:

//admin.py
class GameAdmin(models.ModelAdmin):
    ordering = [F("score").desc(nulls_last=True)]

runserver fails with error: TypeError: argument of type 'OrderBy' is not iterable

Change History (9)

comment:1 Changed 3 years ago by Gabriel Amram

Description: modified (diff)
Summary: Using query expression in model Meta.ordering causes AttributeError in adminUsing query expression in either the model's Meta.ordering or ModelAdmin.ordering causes an error

comment:2 Changed 3 years ago by felixxm

Cc: felixxm added
Triage Stage: UnreviewedAccepted

comment:3 Changed 3 years ago by Tim Graham

Severity: NormalRelease blocker
Summary: Using query expression in either the model's Meta.ordering or ModelAdmin.ordering causes an errorAdmin changelist crashes when using query expression in the model's Meta.ordering or ModelAdmin.ordering

It's a bug in a new feature in 2.0 (#28335).

comment:4 Changed 3 years ago by felixxm

Owner: changed from nobody to felixxm
Status: newassigned

comment:5 Changed 3 years ago by felixxm

Has patch: set

comment:6 Changed 3 years ago by GitHub <noreply@…>

Resolution: fixed
Status: assignedclosed

In c815213:

Fixed #28958 -- Fixed admin changelist crash when using a query expression in the page's ordering.

Thanks Tim Graham for the review.

comment:7 Changed 3 years ago by Mariusz Felisiak <felisiak.mariusz@…>

In b54302d2:

[2.0.x] Fixed #28958 -- Fixed admin changelist crash when using a query expression in the page's ordering.

Thanks Tim Graham for the review.

Backport of c8152137400b5932578cd1788b79560c9772e56b from master

comment:8 Changed 3 years ago by GitHub <noreply@…>

In 1d00923:

Refs #28958 -- Added a test for ModelAdmin with query expressions in ordering.

This provides additional test coverage but isn't a regression test for
the ticket's issue.

comment:9 Changed 3 years ago by Mariusz Felisiak <felisiak.mariusz@…>

In 8f67eea:

[2.0.x] Refs #28958 -- Added a test for ModelAdmin with query expressions in ordering.

This provides additional test coverage but isn't a regression test for
the ticket's issue.

Backport of 1d00923848d504c6132019492b8d5a6cdf8261db from master

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