diff --git a/django/thunderstore/community/models/community.py b/django/thunderstore/community/models/community.py index 113c11b4b..8572fd0a6 100644 --- a/django/thunderstore/community/models/community.py +++ b/django/thunderstore/community/models/community.py @@ -5,7 +5,7 @@ from django.contrib.postgres.fields import ArrayField from django.core.exceptions import ValidationError from django.db import models, transaction -from django.db.models import Manager, QuerySet +from django.db.models import Manager, QuerySet, Sum from django.urls import reverse from django.utils.functional import cached_property @@ -366,7 +366,13 @@ def update_for_community(cls, community: Community) -> None: listings = listings.filter_with_single_community() community.aggregated_fields.package_count = listings.count() - community.aggregated_fields.download_count = sum( - listing.total_downloads for listing in listings + + from thunderstore.repository.models import PackageVersion + + community.aggregated_fields.download_count = ( + PackageVersion.objects.filter( + package_id__in=listings.values("package_id") + ).aggregate(total=Sum("downloads"))["total"] + or 0 ) community.aggregated_fields.save()