Context
I have these two models:
class Country(models.Model):
name = models.CharField(max_length=128, unique=True, validators=[validators.labelvalue])
class City(models.Model):
name = models.CharField(max_length=128, unique=True, validators=[validators.labelvalue])
country = models.ForeignKey('myapp.Country', on_delete=models.CASCADE)
If I’m not mistaken, according to the docs country.city_set
would automatically exist, and country.city_set.count()
will return the number of cities belonging to that country.
related_name
is not used in any of those models.
My problem
Now, in a template, I’m trying to loop through the countries, and I would like them sorted by number of cities. So, this is what I do (simplified):
views.py
from django.views.generic import ListView
class CountryList(ListView):
queryset = models.Country.objects.prefetch_related(
"city_set",
)
template.html
{% for country in country_list|dictsort:"city_set.count" %}
However, is not working and just renders a blank page. If I replace city_set.count
with name
, it works fine, so I guess the problem must be using city_set.count
.
What could be issue? Thanks in advance.