[Fixed]-How to make distinct in django orm extra?

1👍

From the docs https://docs.djangoproject.com/en/1.10/ref/models/querysets/#distinct

On PostgreSQL only, you can pass positional arguments (*fields) in order to specify the names of fields to which the DISTINCT should apply. This translates to a SELECT DISTINCT ON SQL query. Here’s the difference. For a normal distinct() call, the database compares each field in each row when determining which rows are distinct. For a distinct() call with specified field names, the database will only compare the specified field names.

If you only want distinct months in your template, you can specify that you only want months and then do a distinct operation on it.

cekbulan = Transaksi.objects.filter(tanggal__range=["2016-07-01", "2017-06-30"], unit='03').extra(select={'month': "month(date)"}).values('month').distinct()

Leave a comment