#34089 closed Bug (duplicate)

Order by foreign key is not respected when using expression as default ordering

Reported by: Mathieu Rampant Owned by: nobody
Component: Database layer (models, ORM) Version: 3.2
Severity: Normal Keywords: order_by foreign_key
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


I recently switched a bunch of

ordering = ["name"]


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.

Change History (1)

comment:1 by Mariusz Felisiak, 20 months ago

Resolution: duplicate
Status: newclosed

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

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