#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 <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/01070180b94c2f6b-33ef910a-def4-40f6-993d-5957f50bd839-000000%40eu-central-1.amazonses.com.