Skip to content

Commit

Permalink
Add queryset optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
ces92 committed Jan 11, 2025
1 parent 585ac19 commit 3114143
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
12 changes: 9 additions & 3 deletions backend/bvdata/data/api_v1/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ class Meta(ImageSerializerBase.Meta):
pass

def create(self, validated_data):
location_id_string = validated_data.get("location")
location = BaseLocation.objects.get(id_string=location_id_string)
validated_data["location"] = location
validated_data["location"] = BaseLocation.objects.get(
id_string=validated_data.get("location")
)
if (
validated_data.get("description") == ""
or validated_data.get("description") is None
Expand All @@ -78,3 +78,9 @@ class Meta(ImageSerializerBase.Meta):
"location",
"image",
]

def update(self, instance, validated_data):
validated_data["location"] = BaseLocation.objects.get(
id_string=validated_data.get("location")
)
return super(ImageSerializerUpdate, self).update(instance, validated_data)
4 changes: 2 additions & 2 deletions backend/bvdata/data/api_v1/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ class ReviewViewSet(ReadOnlyModelViewSet):


class ImageViewSet(ModelViewSet):
queryset = Image.objects.all()
queryset = Image.objects.all().prefetch_related("location", "uploader")
serializer_class = ImageSerializerDefault
filter_backends = [filters.DjangoFilterBackend]
filterset_class = ImageFilter

def get_serializer_class(self):
if self.request.method in ["PUT", "PATCH"]:
if self.action == "update":
return ImageSerializerUpdate
else:
return ImageSerializerDefault
Expand Down
3 changes: 3 additions & 0 deletions backend/bvdata/data/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ def get_queryset(self):
"boolean_attributes",
"positive_integer_attributes",
"openinghours_set",
"review",
"review__reviewimage_set",
"image_set",
)
.filter(type=self.location_type)
)
Expand Down

0 comments on commit 3114143

Please sign in to comment.