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.
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>
* 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>
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>
* owner: nobody => anegawa-j
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/33682#comment:13>