diff --git a/django/db/models/query.py b/django/db/models/query.py
index 79562ed..9e86872 100644
a
|
b
|
class QuerySet(object):
|
591 | 591 | obj.query.add_ordering(*field_names) |
592 | 592 | return obj |
593 | 593 | |
594 | | def distinct(self, true_or_false=True): |
| 594 | def distinct(self, true_or_false=True, on_fields=None): |
595 | 595 | """ |
596 | 596 | Returns a new QuerySet instance that will select only distinct results. |
597 | 597 | """ |
598 | 598 | obj = self._clone() |
599 | | obj.query.distinct = true_or_false |
| 599 | if on_fields: |
| 600 | if isinstance(on_fields, basestring): |
| 601 | on_fields = (on_fields,) |
| 602 | # Quote the field names |
| 603 | quote = connection.ops.quote_name |
| 604 | table_name = quote(self.model._meta.db_table) |
| 605 | obj.query.distinct = [table_name + "." + quote(field) |
| 606 | for field in on_fields] |
| 607 | else: |
| 608 | obj.query.distinct = true_or_false |
600 | 609 | return obj |
601 | 610 | |
602 | 611 | def extra(self, select=None, where=None, params=None, tables=None, |
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index f4003b2..58bfbb0 100644
a
|
b
|
class BaseQuery(object):
|
408 | 408 | |
409 | 409 | result = ['SELECT'] |
410 | 410 | if self.distinct: |
411 | | result.append('DISTINCT') |
| 411 | if isinstance(self.distinct, list): |
| 412 | result.append('DISTINCT ON (%s)' % ', '.join(self.distinct)) |
| 413 | else: |
| 414 | result.append('DISTINCT') |
412 | 415 | result.append(', '.join(out_cols + self.ordering_aliases)) |
413 | 416 | |
414 | 417 | result.append('FROM') |
… |
… |
class BaseQuery(object):
|
488 | 491 | "Cannot combine queries on two different base models." |
489 | 492 | assert self.can_filter(), \ |
490 | 493 | "Cannot combine queries once a slice has been taken." |
| 494 | # TODO: if distinct lists do not match, this will fail |
| 495 | # whether this should be left as is or treated specially (e.g. by |
| 496 | # merging two lists into one) remains to be decided |
491 | 497 | assert self.distinct == rhs.distinct, \ |
492 | 498 | "Cannot combine a unique query with a non-unique query." |
493 | 499 | |
… |
… |
class BaseQuery(object):
|
912 | 918 | ordering = self.order_by or self.model._meta.ordering |
913 | 919 | qn = self.quote_name_unless_alias |
914 | 920 | qn2 = self.connection.ops.quote_name |
| 921 | # TODO: if self.distinct is a list, it needs to contain all the |
| 922 | # columns used in ordering. Automating this is rather tedious, so |
| 923 | # currently users should be simply warned that their queries will fail |
| 924 | # unless they provide the order_by paramters in distinct as well. |
915 | 925 | distinct = self.distinct |
916 | 926 | select_aliases = self._select_aliases |
917 | 927 | result = [] |
… |
… |
class BaseQuery(object):
|
2120 | 2130 | "Cannot add count col with multiple cols in 'select': %r" % self.select |
2121 | 2131 | count = self.aggregates_module.Count(self.select[0]) |
2122 | 2132 | else: |
| 2133 | # FIXME: the implications of when self.distinct is a list need to |
| 2134 | # be thought through |
2123 | 2135 | opts = self.model._meta |
2124 | 2136 | if not self.select: |
2125 | 2137 | count = self.aggregates_module.Count((self.join((None, opts.db_table, None, None)), opts.pk.column), |