Standard newbie preamble.
I have the following query …
collections_cities = Collection.objects.filter(city_id__gt=0).select_related(‘city’).annotate(Count(
Based on the following models:
class Collection(models.Model): city = models.ForeignKey(City, blank=True, null=True, on_delete=models.CASCADE, verbose_name='Location: City')
I’m using the django-cities-light package:
class City(models.Model): name = models.CharField(max_length=200, db_index=True) display_name = models.CharField(max_length=200) name_ascii = models.CharField(max_length=200, blank=True, db_index=True) slug = autoslug.AutoSlugField(populate_from='name_ascii') geoname_id = models.IntegerField(null=True, blank=True, unique=True) alternate_names = models.TextField(null=True, blank=True, default='')
My ultimate goal is to create a display like the following, with the ? replaced by the number of collections under that city name. I think you would use Annotate and Count, but I’m not having any luck. The query above does not perform the equivalent of distinct(), as show in the screenshot.
Anyone have any guidance to share?