Opened 2 years ago

Last modified 17 months ago

#19842 assigned Cleanup/optimization

annotate()-based solution to distinct and order_by problem

Reported by: jogwen Owned by: avani9330
Component: Documentation Version: 1.4
Severity: Normal Keywords:
Cc: avani9330 Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

https://docs.djangoproject.com/en/1.4/ref/models/querysets/#django.db.models.query.QuerySet.distinct

Note about how distinct may not work when ordering by related model fields could point the reader at this solution http://archlinux.me/dusty/2010/12/07/django-dont-use-distinct-and-order_by-across-relations/, using annotate().

Attachments (3)

add_annotate_suggestion.patch (1.1 KB) - added by jgeskens 2 years ago.
add_annotate_suggestion.2.patch (2.0 KB) - added by jgeskens 2 years ago.
annotate_values_solution.patch (2.3 KB) - added by avani9330@… 20 months ago.
Patch

Download all attachments as: .zip

Change History (23)

comment:1 Changed 2 years ago by dfanders

  • Needs documentation set
  • Needs tests unset
  • Owner changed from nobody to anonymous
  • Patch needs improvement unset
  • Status changed from new to assigned

comment:2 Changed 2 years ago by dfanders

  • Owner changed from anonymous to dfanders

comment:3 Changed 2 years ago by carljm

  • Triage Stage changed from Unreviewed to Accepted

I think it's probably true that many cases of people trying to use distinct and order_by on related fields together would be solved by annotate. It wouldn't hurt to mention annotate in that note.

comment:4 Changed 2 years ago by jgeskens

  • Owner changed from dfanders to anonymous

I also came accross this problem. I will try to put this suggestion in the Documentation as part of the DjangoCon Euro 2013 sprints.

comment:5 Changed 2 years ago by jgeskens

  • Owner changed from anonymous to jgeskens

Changed 2 years ago by jgeskens

comment:6 Changed 2 years ago by jgeskens

  • Has patch set

comment:7 Changed 2 years ago by jonathanslenders

This documentation patch looks fine.

comment:9 Changed 2 years ago by jonathanslenders

  • Triage Stage changed from Accepted to Ready for checkin

comment:10 Changed 2 years ago by jgeskens

  • Triage Stage changed from Ready for checkin to Accepted

The documentation should be improved with an example, as suggested by @timgraham

comment:11 Changed 2 years ago by jgeskens

Fixed a typo in the patch.

Changed 2 years ago by jgeskens

comment:12 Changed 2 years ago by timo

  • Patch needs improvement set

comment:13 Changed 22 months ago by timo

  • Easy pickings unset
  • Owner jgeskens deleted
  • Status changed from assigned to new

Changed 20 months ago by avani9330@…

Patch

comment:14 follow-up: Changed 20 months ago by avani9330@…

  • Triage Stage changed from Accepted to Ready for checkin

comment:15 in reply to: ↑ 14 Changed 20 months ago by avani9330

  • Cc avani9330 added
  • Owner set to avani9330
  • Status changed from new to assigned

comment:16 Changed 20 months ago by avani9330

  • Patch needs improvement unset

comment:17 Changed 20 months ago by avani9330

  • Needs documentation unset

comment:18 Changed 20 months ago by avani9330

  • Triage Stage changed from Ready for checkin to Accepted

comment:20 Changed 17 months ago by mjtamlyn

  • Patch needs improvement set
Note: See TracTickets for help on using tickets.
Back to Top