[Django] #28538: order_with_respect_to not working when model accessed via a many-to-many related field

8 views
Skip to first unread message

Django

unread,
Aug 27, 2017, 1:21:11 PM8/27/17
to django-...@googlegroups.com
#28538: order_with_respect_to not working when model accessed via a many-to-many
related field
-------------------------------------+-------------------------------------
Reporter: Carlos | Owner: nobody
Mermingas |
Type: Bug | Status: new
Component: Database | Version: 1.11
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
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
}}}

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

Django

unread,
Aug 28, 2017, 8:24:29 AM8/28/17
to django-...@googlegroups.com
#28538: order_with_respect_to not working when model accessed via a many-to-many
related field
-------------------------------------+-------------------------------------
Reporter: Carlos Mermingas | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Sep 2, 2017, 2:14:12 PM9/2/17
to django-...@googlegroups.com
#28538: order_with_respect_to not working when model accessed via a many-to-many
related field
-------------------------------------+-------------------------------------
Reporter: Carlos Mermingas | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution: needsinfo
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

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

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


--
Ticket URL: <https://code.djangoproject.com/ticket/28538#comment:2>

Django

unread,
Sep 2, 2017, 5:05:47 PM9/2/17
to django-...@googlegroups.com
#28538: order_with_respect_to not working when model accessed via a many-to-many
related field
-------------------------------------+-------------------------------------
Reporter: Carlos Mermingas | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution: needsinfo
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Sep 2, 2017, 5:47:39 PM9/2/17
to django-...@googlegroups.com
#28538: order_with_respect_to not working when model accessed via a many-to-many
related field
-------------------------------------+-------------------------------------
Reporter: Carlos Mermingas | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

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

* 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>

Reply all
Reply to author
Forward
0 new messages