Collection Speed Improvements (#874)

* Add UltraSlimCollectionSerializer and update CollectionViewSet for optimized listing

- Introduced UltraSlimCollectionSerializer for efficient data representation.
- Updated CollectionViewSet to use the new serializer for list actions.
- Enhanced queryset optimizations with prefetching for related images.
- Modified frontend components to support SlimCollection type for better performance.

* Optimize rendering of collection cards by adding a unique key to the each block
This commit is contained in:
Sean Morley
2025-09-22 08:34:23 -04:00
committed by GitHub
parent 240c617010
commit 8a0f7310b0
7 changed files with 203 additions and 86 deletions

View File

@@ -655,4 +655,49 @@ class CollectionInviteSerializer(serializers.ModelSerializer):
class Meta:
model = CollectionInvite
fields = ['id', 'collection', 'created_at', 'name', 'collection_owner_username', 'collection_user_first_name', 'collection_user_last_name']
read_only_fields = ['id', 'created_at']
read_only_fields = ['id', 'created_at']
class UltraSlimCollectionSerializer(serializers.ModelSerializer):
location_images = serializers.SerializerMethodField()
location_count = serializers.SerializerMethodField()
class Meta:
model = Collection
fields = [
'id', 'user', 'name', 'description', 'is_public', 'start_date', 'end_date',
'is_archived', 'link', 'created_at', 'updated_at', 'location_images',
'location_count', 'shared_with'
]
read_only_fields = fields # All fields are read-only for listing
def get_location_images(self, obj):
"""Get primary images from locations in this collection, optimized with select_related"""
# Filter first, then slice (removed slicing)
images = ContentImage.objects.filter(
location__collections=obj
).select_related('user').prefetch_related('location')
return ContentImageSerializer(
images,
many=True,
context={'request': self.context.get('request')}
).data
def get_location_count(self, obj):
"""Get count of locations in this collection"""
# This uses the cached count if available, or does a simple count query
return obj.locations.count()
def to_representation(self, instance):
representation = super().to_representation(instance)
# make it show the uuid instead of the pk for the user
representation['user'] = str(instance.user.uuid)
# Make it display the user uuid for the shared users instead of the PK
shared_uuids = []
for user in instance.shared_with.all():
shared_uuids.append(str(user.uuid))
representation['shared_with'] = shared_uuids
return representation