Ticket #6422: ticket6422.diff

File ticket6422.diff, 3.3 KB (added by mrts, 14 years ago)

Update patch to 1.1.X

  • django/db/models/query.py

    diff --git a/django/db/models/query.py b/django/db/models/query.py
    index 79562ed..9e86872 100644
    a b class QuerySet(object):  
    591591        obj.query.add_ordering(*field_names)
    592592        return obj
    593593
    594     def distinct(self, true_or_false=True):
     594    def distinct(self, true_or_false=True, on_fields=None):
    595595        """
    596596        Returns a new QuerySet instance that will select only distinct results.
    597597        """
    598598        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
    600609        return obj
    601610
    602611    def extra(self, select=None, where=None, params=None, tables=None,
  • django/db/models/sql/query.py

    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):  
    408408
    409409        result = ['SELECT']
    410410        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')
    412415        result.append(', '.join(out_cols + self.ordering_aliases))
    413416
    414417        result.append('FROM')
    class BaseQuery(object):  
    488491                "Cannot combine queries on two different base models."
    489492        assert self.can_filter(), \
    490493                "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
    491497        assert self.distinct == rhs.distinct, \
    492498            "Cannot combine a unique query with a non-unique query."
    493499
    class BaseQuery(object):  
    912918            ordering = self.order_by or self.model._meta.ordering
    913919        qn = self.quote_name_unless_alias
    914920        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.
    915925        distinct = self.distinct
    916926        select_aliases = self._select_aliases
    917927        result = []
    class BaseQuery(object):  
    21202130                        "Cannot add count col with multiple cols in 'select': %r" % self.select
    21212131                count = self.aggregates_module.Count(self.select[0])
    21222132        else:
     2133            # FIXME: the implications of when self.distinct is a list need to
     2134            # be thought through
    21232135            opts = self.model._meta
    21242136            if not self.select:
    21252137                count = self.aggregates_module.Count((self.join((None, opts.db_table, None, None)), opts.pk.column),
Back to Top