What we want to do here is:
1. We want distinct foreign key from the records
2. We want this unique foreign key contains number of records that belongs to.
Example:
Country -> has many -> Insurance
1 2 3 4 5 | Country(models.Model): name = models.Charfields(..) Insurance(model.Model): country = models.ForeignKey(Country) |
We want to distinct Country in Insurance and show numbers of insurance in each unique Country.
1 | Insurance.objects.order_by("country").values_list("country__name").distinct().annotate(Count("id")) |