Optimize product count retrieval and enhance query performance with prefetch_related
This commit is contained in:
@@ -102,7 +102,9 @@ class SubCategorySerializer(serializers.ModelSerializer):
|
||||
'meta_description', 'product_count', 'parent', 'image']
|
||||
|
||||
def get_product_count(self, obj):
|
||||
return obj.products.count()
|
||||
# Use annotated product_count if available (from optimized query)
|
||||
# Otherwise fall back to counting (for backward compatibility)
|
||||
return getattr(obj, 'product_count', obj.products.count())
|
||||
|
||||
def get_parent(self, obj):
|
||||
return obj.parent.name
|
||||
|
||||
Reference in New Issue
Block a user