Skip to content

Commit

Permalink
feat: 평균 별점 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
dkfla committed Aug 2, 2024
1 parent 0c31cc2 commit c559f15
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
25 changes: 20 additions & 5 deletions restaurants/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@


class RestaurantSerializer(serializers.ModelSerializer):
reviews = serializers.SerializerMethodField()
rating_average = serializers.SerializerMethodField()

class Meta:
model = Restaurant
fields = "__all__"

def get_reviews(self, obj):
reviews = obj.reviews.order_by("-recommend_count")[:4]
serializer = ReviewSerializer(reviews, many=True)
return serializer.data
def get_rating_average(self, obj):
return obj.rating_average()


class RestaurantListSerializer(serializers.ModelSerializer):
Expand Down Expand Up @@ -44,3 +42,20 @@ class UserRestaurantListSerializer(serializers.ModelSerializer):
class Meta:
model = UserRestaurantsList
fields = "__all__"


class RestaurantDetailSerializer(serializers.ModelSerializer):
reviews = serializers.SerializerMethodField()
rating_average = serializers.SerializerMethodField()

class Meta:
model = Restaurant
fields = "__all__"

def get_reviews(self, obj):
reviews = obj.reviews.order_by("-recommend_count")[:4]
serializer = ReviewSerializer(reviews, many=True)
return serializer.data

def get_rating_average(self, obj):
return obj.rating_average()
3 changes: 2 additions & 1 deletion restaurants/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
RestaurantListSerializer,
SearchHistorySerializer,
UserRestaurantListSerializer,
RestaurantDetailSerializer,
)

# from django.contrib.auth.decorators import login_required
Expand Down Expand Up @@ -108,7 +109,7 @@ def add_remove_restaurant(request, pk):
def restaurant_detail(request, pk):
try:
restaurant = Restaurant.objects.prefetch_related("reviews").get(pk=pk)
serializer = RestaurantSerializer(restaurant)
serializer = RestaurantDetailSerializer(restaurant)
return Response(serializer.data)
except Restaurant.DoesNotExist:
return Response(
Expand Down

0 comments on commit c559f15

Please sign in to comment.