#28538 closed Bug (invalid)
order_with_respect_to not working when model accessed via a many-to-many related field
Reported by: | Carlos Mermingas | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.11 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Please let me know if I am doing something wrong or my expectation is incorrect. Also, if this ends up being a confirmed problem and an "easy picking", I'd be happy to try to fix it.
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
Change History (4)
follow-up: 3 comment:1 by , 7 years ago
comment:2 by , 7 years ago
Resolution: | → needsinfo |
---|---|
Status: | new → closed |
comment:3 by , 7 years ago
Replying to Tim Graham:
Why do you expect ordering on the second query? It's retrieving
Plate
rather thanMenuPlate
.
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.
comment:4 by , 7 years ago
Resolution: | needsinfo → invalid |
---|
No problem. By the way, it's more appropriate to ask "is it bug? questions using our support channels.
Why do you expect ordering on the second query? It's retrieving
Plate
rather thanMenuPlate
.