--- query.py.orig	2008-01-19 14:37:37.000000000 +0100
+++ query.py	2008-01-19 14:59:23.000000000 +0100
@@ -186,8 +186,20 @@
         extra_select = self._select.items()
 
         cursor = connection.cursor()
-        cursor.execute("SELECT " + (self._distinct and "DISTINCT " or "") + ",".join(select) + sql, params)
-
+        select, sql, params = self._get_sql_clause()
+        if self._distinct == False:
+            distinct = ""
+        elif self._distinct == True:
+            distinct = "DISTINCT "
+        else:
+            # CHECKME: probably need some qouting around the %s (or use %r ???)
+            distinct = "DISTINCT ON (%s) " % (
+                # CHECKME: the type check would not be necessary if the
+                # distinct method would ensure to store a tuple in
+                # self._distinct in any case.
+                type(self._distinct) == type('') and self._distinct
+                or ','.join(self._distinct))
+        cursor.execute("SELECT " + distinct + ",".join(select) + sql, params)
         fill_cache = self._select_related
         fields = self.model._meta.fields
         index_end = len(fields)
@@ -236,6 +248,8 @@
 
         cursor = connection.cursor()
         if self._distinct:
+            # CHECKME: may have to change this for DISTINCT ON queries, i.e.
+            # when self._distinct is not 'True' but a string or tuple
             id_col = "%s.%s" % (connection.ops.quote_name(self.model._meta.db_table),
                     connection.ops.quote_name(self.model._meta.pk.column))
             cursor.execute("SELECT COUNT(DISTINCT(%s))" % id_col + sql, params)
@@ -421,9 +435,16 @@
                 "Cannot reorder a query once a slice has been taken."
         return self._clone(_order_by=field_names)
 
-    def distinct(self, true_or_false=True):
+    def distinct(self, true_or_false=True, *fields):
         "Returns a new QuerySet instance with '_distinct' modified."
-        return self._clone(_distinct=true_or_false)
+        if fields:
+            # CHECKME: if fields is a one-tuple a simple string will be stored
+            # in self._distinct in the cloned object. This leads to more
+            # complex precessing when the value is used so it would be
+            # beneficial to make sure to always store a tuple here.
+            return self._clone(_distinct=fields)
+        else:
+            return self._clone(_distinct=true_or_false)
 
     def extra(self, select=None, where=None, params=None, tables=None):
         assert self._limit is None and self._offset is None, \
@@ -462,6 +483,9 @@
         assert self._limit is None and self._offset is None \
             and other._limit is None and other._offset is None, \
             "Cannot combine queries once a slice has been taken."
+        # CHECKME: for DISTINCT ON queries this will always return false
+        # unless both field sets are identical. I don't know if this is
+        # correct, if so the error message may have to be adapted.
         assert self._distinct == other._distinct, \
             "Cannot combine a unique query with a non-unique query"
         #  use 'other's order by
@@ -617,7 +641,16 @@
             field_names.extend([f[0] for f in extra_select])
 
         cursor = connection.cursor()
-        cursor.execute("SELECT " + (self._distinct and "DISTINCT " or "") + ",".join(select) + sql, params)
+        if self._distinct == False:
+            distinct = ""
+        elif self._distinct == True:
+            distinct = "DISTINCT "
+        else:
+            # CHECKME: see above (same as in the QuerySet class)
+            distinct = "DISTINCT ON (%s) " % (
+                type(self._distinct) == type('') and self._distinct
+                or ','.join(self._distinct))
+        cursor.execute("SELECT " + distinct + ",".join(select) + sql, params)
 
         has_resolve_columns = hasattr(self, 'resolve_columns')
         while 1:
