[Django] #34089: Order by foreign key is not respected when using expression as default ordering

5 views
Skip to first unread message

Django

unread,
Oct 12, 2022, 12:11:09 PM10/12/22
to django-...@googlegroups.com
#34089: Order by foreign key is not respected when using expression as default
ordering
-------------------------------------+-------------------------------------
Reporter: rptmat57 | Owner: nobody
Type: Bug | Status: new
Component: Database | Version: 3.2
layer (models, ORM) | Keywords: order_by
Severity: Normal | foreign_key
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
I recently switched a bunch of
{{{
ordering = ["name"]
}}}

to

{{{
ordering = [Lower("name")]
}}}

in the Meta class of some models.

Now when I order a related Model, the result is not order as it should be.

Consider the following example:

{{{
class ItemCategory(models.Model):
name = models.CharField(max_length=100)

class Meta:
ordering = [Lower("name")]

class Item(models.Model):
name = models.CharField(max_length=100)
category = models.ForeignKey("ItemCategory", blank=True, null=True,
on_delete=models.CASCADE)

class Meta:
ordering = [Lower("name")]
}}}

If I do `Item.objects.all().order_by("category", "name")` then the results
are ordered by Item name only, not ItemCategory name and Item name as it
should be.

--
Ticket URL: <https://code.djangoproject.com/ticket/34089>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Oct 12, 2022, 12:22:35 PM10/12/22
to django-...@googlegroups.com
#34089: Order by foreign key is not respected when using expression as default
ordering
-------------------------------------+-------------------------------------
Reporter: rptmat57 | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution: duplicate
Keywords: order_by | Triage Stage:
foreign_key | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => duplicate


Comment:

Duplicate of #29538, fixed in Django 4.1 (see
2798c937deb6625a4e6a36e70d4d60ce5faac954).

--
Ticket URL: <https://code.djangoproject.com/ticket/34089#comment:1>

Reply all
Reply to author
Forward
0 new messages