1👍
✅
You can simply use .distinct()
Flat.objects.order_by('flat_name').distinct('flat_name')
**EDIT: **
Above code will work only for PostgreSQL.
0👍
For each block
block.flat_set().all()
should give you all the flats of that block. Then you can leave only the unique ones using distinct()
. All together:
block.flat_set().all().distinct('flat_name')
You can read about it here:
https://docs.djangoproject.com/en/1.9/topics/db/queries/#following-relationships-backward
Source:stackexchange.com