--- a/lib/sqlalchemy/orm/query.py +++ b/lib/sqlalchemy/orm/query.py @@ -1255,12 +1255,27 @@ class Query(object): self._offset = offset @_generative(_no_statement_condition) - def distinct(self): + @util.accepts_a_list_as_starargs(list_deprecation='pending') + def distinct(self, *criterion): """Apply a ``DISTINCT`` to the query and return the newly resulting ``Query``. + Can accept list of expressions to construct postgresql DISTINCT ON clause + or None to remove it from query. + """ - self._distinct = True + + if not criterion: + self._distinct = True + if len(criterion) == 1 and criterion[0] is None: + self._distinct = False + else: + criterion = [self._adapt_clause(expression._literal_as_text(o), True, True) for o in criterion] + + if not isinstance(self._distinct, bool): + self._distinct += criterion + else: + self._distinct = criterion def all(self): """Return the results represented by this ``Query`` as a list.