The patch included modifies django/db/models/query.py so that the distinct method of the QuerySet object optionally takes variable number of field names and, if given, modifies the SQL statements generated so that 'DISTINCT ON (field,...)' is used. The incentive is to allow things like described in the following example:
class Example(models.Model):
name = models.TextField()
date = models. DateTimeField()
other = models.XXX()
...
qs = Example.objects.all().distinct('name').order_by('name','-date')
Now qs will return the latest entry for each distinct name. This cannot otherwise be achieved unless resorting to plain SQL.
It should be noted that - at least in standard SQL and Postgres - if name allows NULL values, the query will return all entries for which name is NULL not only the latest one as one might expect.
There will be a couple of '# CHECKME:' comments scattered over the patched file. I tried to mark all places where the change might have a side effect but it wasn't apparent to me if and how to fix it. I.e. there should be no side effect from the code outside the commented regions.