Code

Opened 17 months ago

Last modified 5 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 14 months ago.
add_annotate_suggestion.2.patch (2.0 KB) - added by jgeskens 14 months ago.
annotate_values_solution.patch (2.3 KB) - added by avani9330@… 9 months ago.
Patch

Download all attachments as: .zip

Change History (23)

comment:1 Changed 17 months 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 17 months ago by dfanders

  • Owner changed from anonymous to dfanders

comment:3 Changed 17 months 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 14 months 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 14 months ago by jgeskens

  • Owner changed from anonymous to jgeskens

Changed 14 months ago by jgeskens

comment:6 Changed 14 months ago by jgeskens

  • Has patch set

comment:7 Changed 14 months ago by jonathanslenders

This documentation patch looks fine.

comment:9 Changed 14 months ago by jonathanslenders

  • Triage Stage changed from Accepted to Ready for checkin

comment:10 Changed 14 months 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 14 months ago by jgeskens

Fixed a typo in the patch.

Changed 14 months ago by jgeskens

comment:12 Changed 14 months ago by timo

  • Patch needs improvement set

comment:13 Changed 10 months ago by timo

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

Changed 9 months ago by avani9330@…

Patch

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

  • Triage Stage changed from Accepted to Ready for checkin

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

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

comment:16 Changed 8 months ago by avani9330

  • Patch needs improvement unset

comment:17 Changed 8 months ago by avani9330

  • Needs documentation unset

comment:18 Changed 8 months ago by avani9330

  • Triage Stage changed from Ready for checkin to Accepted

comment:20 Changed 5 months ago by mjtamlyn

  • Patch needs improvement set

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as assigned
The owner will be changed from avani9330 to anonymous. Next status will be 'assigned'
The ticket will be disowned. Next status will be 'new'
as The resolution will be set. Next status will be 'closed'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.