﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
6422	Support for 'DISTINCT ON' queries with QuerySet.distinct()	Manfred Wassmann <manolo@…>	Jeffrey Gelens	"The patch included modifies django/db/models/query.py so that the distinct method of the !QuerySet object optionally takes variable number of field names and, if given, modifies the SQL statements generated so that 'DISTINCT ON (field,...)' is used. The incentive is to allow things like described in the following example:

{{{
class Example(models.Model):
  name = models.TextField()
  date = models. DateTimeField()
  other = models.XXX()
  ...

qs = Example.objects.all().distinct('name').order_by('name','-date')
}}}

Now qs will return the latest entry for each distinct name. This cannot otherwise be achieved unless resorting to plain SQL.

It should be noted that - at least in standard SQL and Postgres - if name allows NULL values, the query will return __all__ entries for which name is NULL not only the latest one as one might expect.

There will be a couple of '# CHECKME:' comments scattered over the patched file. I tried to mark all places where the change might have a side effect but it wasn't apparent to me if and how to fix it. I.e. there should be no side effect from the code outside the commented regions. "	New feature	closed	Database layer (models, ORM)	dev	Normal	fixed	dceu2011	cornbread ramusus@… tim.babych@… hr.bjarni+django@… jeffrey@… James Pic taylor.mitchell@… diegobz jonas-django@… anssi.kaariainen@… German M. Bravo	Ready for checkin	1	0	0	0	0	0
