Ticket #6422: #6422-distinct.diff
File #6422-distinct.diff, 1.9 KB (added by , 13 years ago) |
---|
-
django/db/models/query.py
673 673 obj.query.add_ordering(*field_names) 674 674 return obj 675 675 676 def distinct(self, true_or_false=True ):676 def distinct(self, true_or_false=True, on_fields=None): 677 677 """ 678 678 Returns a new QuerySet instance that will select only distinct results. 679 679 """ 680 680 obj = self._clone() 681 obj.query.distinct = true_or_false 681 if on_fields: 682 if isinstance(on_fields, basestring): 683 on_fields = (on_fields,) 684 obj.query.distinct = on_fields 685 else: 686 obj.query.distinct = true_or_false 682 687 return obj 683 688 684 689 def extra(self, select=None, where=None, params=None, tables=None, … … 1098 1103 """ 1099 1104 return self 1100 1105 1101 def distinct(self, true_or_false=True ):1106 def distinct(self, true_or_false=True, on_fields=None): 1102 1107 """ 1103 1108 Always returns EmptyQuerySet. 1104 1109 """ -
django/db/models/sql/compiler.py
75 75 76 76 result = ['SELECT'] 77 77 if self.query.distinct: 78 result.append('DISTINCT') 78 if isinstance(self.query.distinct, (list, tuple)): 79 # Quote the field names 80 qn = self.connection.ops.quote_name 81 db_table = qn(self.query.model._meta.db_table) 82 distinct = [db_table + "." + qn(name) 83 for name in self.query.distinct] 84 result.append('DISTINCT ON (%s)' % ', '.join(distinct)) 85 else: 86 result.append('DISTINCT') 79 87 result.append(', '.join(out_cols + self.query.ordering_aliases)) 80 88 81 89 result.append('FROM')