Hey there!
Maybe you can get around this overriding the get_queryset method.
from django.db.models import Count
@admin.register(Country)
class CountryAdmin(admin.ModelAdmin):
list_display = ('id', 'name', 'get_cities', 'iso2', 'iso3')
@admin.display(description='Cities', ordering='city')
def get_cities(self, obj):
# count_results = obj.city_set.count()
# this is no longer needed, since the value is annotated on the queryset
return format_html('<a href="{0}?country__id={1}">{2}</a>'.format( reverse("admin:myapp_city_changelist"), obj.pk, obj.city_count ))
def get_queryset(self, request):
return super().get_queryset(request).annotate(city_count=Count("city_set")).order_by("-city_count")
unfortunately it doesn’t work due to following errors:
Cannot resolve keyword ‘city_set’ into field. Choices are: continent, id, iso2, iso3, iso_numeric, name, tld, city. If I edit city_set to city I obtain following error
That’s strange, i used to do this a lot. Can you add this to your country fk?
class City(models.Model):
name = models.CharField(max_length=150, help_text="City name")
country = models.ForeignKey('Country', on_delete=models.SET_NULL, null=True, blank=True,
related_name="cities" # <- This
)
# On the admin get_queryset
def get_queryset(self, request):
return super().get_queryset(request).annotate(city_count=Count("cities")).order_by("-city_count")
Now it works!
So, automatic ordering does not seems to work in @admin.display(description='Cities', ordering='cities_count'). Howewer, If I click on “cities” column 3 times it works.
Furthermore, I had to put cities_count in ordering since if I put cities, I have the duplicates.