Ticket #6422: #6422-distinct.diff

File #6422-distinct.diff, 1.9 KB (added by German M. Bravo, 13 years ago)

Patch updated for 1.3

  • django/db/models/query.py

     
    673673        obj.query.add_ordering(*field_names)
    674674        return obj
    675675
    676     def distinct(self, true_or_false=True):
     676    def distinct(self, true_or_false=True, on_fields=None):
    677677        """
    678678        Returns a new QuerySet instance that will select only distinct results.
    679679        """
    680680        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
    682687        return obj
    683688
    684689    def extra(self, select=None, where=None, params=None, tables=None,
     
    10981103        """
    10991104        return self
    11001105
    1101     def distinct(self, true_or_false=True):
     1106    def distinct(self, true_or_false=True, on_fields=None):
    11021107        """
    11031108        Always returns EmptyQuerySet.
    11041109        """
  • django/db/models/sql/compiler.py

     
    7575
    7676        result = ['SELECT']
    7777        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')
    7987        result.append(', '.join(out_cols + self.query.ordering_aliases))
    8088
    8189        result.append('FROM')
Back to Top