Re: [Django] #33682: Clarify using distinct() with related fields that have Meta.ordering defined.

10 views
Skip to first unread message

Django

unread,
May 12, 2022, 11:30:35 AM5/12/22
to django-...@googlegroups.com
#33682: Clarify using distinct() with related fields that have Meta.ordering
defined.
--------------------------------------+------------------------------------
Reporter: Robert Leach | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: 3.2
Severity: Normal | Resolution:
Keywords: sql, distinct | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------

Comment (by Robert Leach):

That's definitely better, as it directly references Meta.ordering, which
is a great clue to what the implications are. What do you think about
**adding** something like:

> If all you want is to make a set of query results distinct without
changing the ordering, note you must explicitly "re-"add the otherwise
over-ridden fields defined in Meta.ordering. But be careful, if you
simply add those fields, you can run afoul of the matching fields
requirement between order_by and distinct. The field(s) defined in
Meta.ordering can include a foreign key, which will resolve differently in
order_by (to the related model's Meta.ordering field(s)) and distinct (the
`_id`) and the fields will no longer match between the 2 expressions.
>
> But this gotcha can be entirely avoided by only ever using real database
fields in every `Meta.ordering`, `order_by`, and `distinct` list instead
of adding Django's related model objects (e.g. `ForeignKey`,
`ManyToManyField`, `OneToManyField`, etc.) so as not to rely on Django's
differing related-model-object to database-field resolving mechanisms
(e.g. instead of adding `blog` to `Entry.Meta.ordering`, add
`blog__name`).

...but like I said, all the info is there to either put it together and
anticipate it (albeit requiring a fairly deep understanding and more
experience to achieve those insights), or to interpret the exception. I
just feel like piecing together this (what I would consider common) use-
case, may make it clearer to those who are endeavoring into their first
big Django project and applying complex understandings of and experiences
with other programming languages.

Whatever edit gets included, I would like to say that I very much
appreciate your attention to this topic.

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

Django

unread,
May 12, 2022, 1:23:42 PM5/12/22
to django-...@googlegroups.com
#33682: Clarify using distinct() with related fields that have Meta.ordering
defined.
--------------------------------------+------------------------------------
Reporter: Robert Leach | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: 3.2
Severity: Normal | Resolution:
Keywords: sql, distinct | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------

Comment (by Robert Leach):

OK, I just learned that you **cannot** add something like `blog__name` to
Entry's `Meta.ordering`. Still learning... So...

> If all you want is to make a set of query results distinct without
changing the ordering, note you must explicitly "re-"add the otherwise
over-ridden fields defined in Meta.ordering. But be careful, if you simply
add those fields, you can run afoul of the matching fields requirement
between order_by and distinct. The field(s) defined in Meta.ordering can

include a foreign key (via `ForeignKey`, `ManyToManyField`,
`OneToManyField`, etc. fields), which will resolve differently in order_by
(to the related model's Meta.ordering field(s)) and distinct (the _id) and


the fields will no longer match between the 2 expressions.
>

> Fields of related models cannot be added to a Model's `Meta.ordering`
(e.g. you cannot add `blog__name` to `Entry.Meta.ordering`), so to avoid
the order_by versus distinct matching field gotcha **and** retain the
default ordering in this instance, there are no shortcuts to only apply
distinct to an existing query without explicitly re-applying the default
ordering and resolving related model objects to database fields.

--
Ticket URL: <https://code.djangoproject.com/ticket/33682#comment:10>

Django

unread,
May 13, 2022, 2:06:21 AM5/13/22
to django-...@googlegroups.com
#33682: Clarify using distinct() with related fields that have Meta.ordering
defined.
--------------------------------------+------------------------------------
Reporter: Robert Leach | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: 3.2
Severity: Normal | Resolution:
Keywords: sql, distinct | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Mariusz Felisiak):

* cc: Carlton Gibson (added)


Comment:

It seems we need a second option about the wording.

--
Ticket URL: <https://code.djangoproject.com/ticket/33682#comment:11>

Django

unread,
May 13, 2022, 3:41:21 AM5/13/22
to django-...@googlegroups.com
#33682: Clarify using distinct() with related fields that have Meta.ordering
defined.
--------------------------------------+------------------------------------
Reporter: Robert Leach | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: 3.2
Severity: Normal | Resolution:
Keywords: sql, distinct | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------

Comment (by Carlton Gibson):

I think Mariusz' suggestion is more appropriate. I don't think the more
discursive "gotcha"-type content fits in the reference documentation,
rather than a more tutorial like piece, where it would be better. (Perhaps
a blog post if there's no spot we yet discuss this in that kind of
context.)

I think the **solution** here comes back to Simon's comment:1…

> ...error out loudly so at least users are not presented this cryptic
error.

If we could catch this and present an error showing the field mismatch
folks would be able to correct at source. (That's totally Blue Skies — no
idea how easy building a better error message is here.)

(I think like Mariusz, I'd lean heavily towards the ''raise'' option,
rather than trying to implicitly do/guess the right thing™ here.)

HTH

--
Ticket URL: <https://code.djangoproject.com/ticket/33682#comment:12>

Django

unread,
Nov 15, 2022, 9:51:42 PM11/15/22
to django-...@googlegroups.com
#33682: Clarify using distinct() with related fields that have Meta.ordering
defined.
-------------------------------------+-------------------------------------
Reporter: Robert Leach | Owner: anegawa-j
Type: | Status: assigned
Cleanup/optimization |

Component: Documentation | Version: 3.2
Severity: Normal | Resolution:
Keywords: sql, distinct | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by anegawa-j):

* owner: nobody => anegawa-j
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/33682#comment:13>

Django

unread,
Feb 13, 2026, 4:07:26 PM (22 hours ago) Feb 13
to django-...@googlegroups.com
#33682: Clarify using distinct() with related fields that have Meta.ordering
defined.
-------------------------------------+-------------------------------------
Reporter: Robert Leach | Owner: Abhimanyu
Type: | Singh Negi
Cleanup/optimization | Status: assigned
Component: Documentation | Version: 3.2
Severity: Normal | Resolution:
Keywords: sql, distinct | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Abhimanyu Singh Negi):

* owner: anegawa-j => Abhimanyu Singh Negi

Comment:

I'd like to work on this ticket.
As the last contributor hasnt made any changes in 3yrs assigning to myself
--
Ticket URL: <https://code.djangoproject.com/ticket/33682#comment:14>

Django

unread,
Feb 13, 2026, 4:35:09 PM (21 hours ago) Feb 13
to django-...@googlegroups.com
#33682: Clarify using distinct() with related fields that have Meta.ordering
defined.
-------------------------------------+-------------------------------------
Reporter: Robert Leach | Owner: Abhimanyu
Type: | Singh Negi
Cleanup/optimization | Status: assigned
Component: Documentation | Version: 3.2
Severity: Normal | Resolution:
Keywords: sql, distinct | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Abhimanyu Singh Negi):

* has_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/33682#comment:15>

Django

unread,
Feb 13, 2026, 4:55:30 PM (21 hours ago) Feb 13
to django-...@googlegroups.com
#33682: Clarify using distinct() with related fields that have Meta.ordering
defined.
-------------------------------------+-------------------------------------
Reporter: Robert Leach | Owner: Abhimanyu
Type: | Singh Negi
Cleanup/optimization | Status: assigned
Component: Documentation | Version: 3.2
Severity: Normal | Resolution:
Keywords: sql, distinct | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* has_patch: 1 => 0

--
Ticket URL: <https://code.djangoproject.com/ticket/33682#comment:16>

Django

unread,
Feb 13, 2026, 5:07:19 PM (21 hours ago) Feb 13
to django-...@googlegroups.com
#33682: Clarify using distinct() with related fields that have Meta.ordering
defined.
-------------------------------------+-------------------------------------
Reporter: Robert Leach | Owner: Abhimanyu
Type: | Singh Negi
Cleanup/optimization | Status: assigned
Component: Documentation | Version: 3.2
Severity: Normal | Resolution:
Keywords: sql, distinct | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Abhimanyu Singh Negi):

* has_patch: 0 => 1

Comment:

https://github.com/django/django/pull/20698

pr raised for this issue
--
Ticket URL: <https://code.djangoproject.com/ticket/33682#comment:17>
Reply all
Reply to author
Forward
0 new messages