Consider the following models for restaurant menus:
{{{
class Plate(models.Model):
name = models.CharField(max_length=100)
class Menu(models.Model):
name = models.CharField(max_length=100)
plates = models.ManyToManyField(Plate, through='MenuPlate')
class MenuPlate(models.Model):
menu = models.ForeignKey(Menu, on_delete=models.CASCADE)
plate = models.ForeignKey(Plate, on_delete=models.CASCADE)
class Meta:
order_with_respect_to = 'menu'
}}}
In the Django shell, after creating a few objects, notice how the first
query has an `order by` but the second query doesn't:
{{{
# from menus.models import Menu, Plate, MenuPlate
# m = Menu.objects.all().first()
# print(MenuPlate.objects.filter(menu=m).query)
SELECT "menus_menuplate"."id", "menus_menuplate"."menu_id",
"menus_menuplate"."plate_id", "menus_menuplate"."_order" FROM
"menus_menuplate" WHERE "menus_menuplate"."menu_id" = 1 ORDER BY
"menus_menuplate"."_order" ASC
# print(m.plates.all().query)
SELECT "menus_plate"."id", "menus_plate"."name" FROM "menus_plate" INNER
JOIN "menus_menuplate" ON ("menus_plate"."id" =
"menus_menuplate"."plate_id") WHERE "menus_menuplate"."menu_id" = 1
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/28538>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Comment (by Tim Graham):
Why do you expect ordering on the second query? It's retrieving `Plate`
rather than `MenuPlate`.
--
Ticket URL: <https://code.djangoproject.com/ticket/28538#comment:1>
* status: new => closed
* resolution: => needsinfo
--
Ticket URL: <https://code.djangoproject.com/ticket/28538#comment:2>
Comment (by Carlos Mermingas):
Replying to [comment:1 Tim Graham]:
> Why do you expect ordering on the second query? It's retrieving `Plate`
rather than `MenuPlate`.
Tim, I apologize for the delay in responding. I missed the notification
e-mail when you asked the question.
After better understanding things, I realize that this is not a bug.
In any case, my requirement is to sort plates within a menu. So, I expect
that whenever I get a list of `MenuPlate` objects, they are ordered with
respect to the `Menu` they belong to. Although the second query returns
instances of `Plate`, it is doing so by going (behind the scenes) through
the intermediate model, `MenuPlate`, which defines an ordering. I would
expect this ordering to be used.
Now, if this was the way things worked, I am not sure what should happen
if `MenuPlate` and `Plate` both defined an ordering.
Thank you for taking the time to look into this. If you believe this is
something that merits consideration, I'll be happy to elaborate this
discussion and I'll pay more attention to the notification e-mails.
--
Ticket URL: <https://code.djangoproject.com/ticket/28538#comment:3>
* resolution: needsinfo => invalid
Comment:
No problem. By the way, it's more appropriate to ask "is it bug? questions
using [wiki:TicketClosingReasons/UseSupportChannels our support channels].
--
Ticket URL: <https://code.djangoproject.com/ticket/28538#comment:4>