From ffbb40e7d947752e331c0edf12af57bb2d31a25b Mon Sep 17 00:00:00 2001 From: jhyns Date: Thu, 18 May 2023 11:02:46 +0900 Subject: [PATCH] Remove color in post model, optimize queryset --- market/admin.py | 1 - market/models.py | 1 - market/serializers.py | 39 +++++++++++++++++++-------------------- market/viewsets.py | 15 +++++++++------ 4 files changed, 28 insertions(+), 28 deletions(-) diff --git a/market/admin.py b/market/admin.py index bb224ea..d941b3d 100644 --- a/market/admin.py +++ b/market/admin.py @@ -13,7 +13,6 @@ admin.site.register(Image) class PostAdmin(admin.ModelAdmin): list_display = ( "product", - "color", "storage", "price", "text", diff --git a/market/models.py b/market/models.py index a94773b..2652638 100644 --- a/market/models.py +++ b/market/models.py @@ -43,7 +43,6 @@ class Post(models.Model): product = models.ForeignKey( Product, on_delete=models.CASCADE, related_name="posts" ) - color = models.CharField(max_length=20) storage = models.ForeignKey(ProductStorage, on_delete=models.CASCADE) price = models.IntegerField() text = models.TextField() diff --git a/market/serializers.py b/market/serializers.py index f0dbfb7..3ef21da 100644 --- a/market/serializers.py +++ b/market/serializers.py @@ -4,38 +4,38 @@ from rest_framework.serializers import ModelSerializer from market.models import ( Brand, Product, - ProductStorage, Post, Image, ) -class BrandSerializer(ModelSerializer): - class Meta: - model = Brand - fields = "__all__" - - -class ProductStorageSerializer(ModelSerializer): - class Meta: - model = ProductStorage - fields = ("storage",) - - class ProductSerializer(ModelSerializer): - storage = ProductStorageSerializer(many=True, read_only=True) + storage = serializers.StringRelatedField(many=True) class Meta: model = Product fields = ("id", "name", "brand", "storage") + depth = 1 class ProductListSerializer(ModelSerializer): - brand = serializers.CharField(source="brand.name") - class Meta: model = Product - fields = ("id", "name", "brand") + fields = ("id", "name") + + +class BrandSerializer(ModelSerializer): + products = ProductListSerializer(many=True, read_only=True) + + class Meta: + model = Brand + fields = ["id", "name", "products"] + + +class BrandListSerializer(ModelSerializer): + class Meta: + model = Brand + fields = "__all__" class ImageSerializer(ModelSerializer): @@ -48,15 +48,14 @@ class PostSerializer(ModelSerializer): product = ProductListSerializer(read_only=True) nickname = serializers.CharField(source="author.nickname") images = ImageSerializer(many=True, read_only=True) - # storage = serializers.CharField(source="storage.storage") + storage = serializers.CharField(source="storage.storage") class Meta: model = Post fields = ( "id", "product", - "color", - "storage__storage", + "storage", "price", "text", "nickname", diff --git a/market/viewsets.py b/market/viewsets.py index 710cedc..aceb09d 100644 --- a/market/viewsets.py +++ b/market/viewsets.py @@ -30,7 +30,11 @@ class BrandViewset(ActionBasedMixin, ModelViewSet): class ProductViewset(ActionBasedMixin, ModelViewSet): - queryset = Product.objects.all() + queryset = ( + Product.objects.all() + .select_related("brand") + .prefetch_related("storage") + ) serializer_class = ProductSerializer serializer_class_map = { "posts": PostSerializer, @@ -52,11 +56,10 @@ class ProductViewset(ActionBasedMixin, ModelViewSet): class PostViewset(ActionBasedMixin, ModelViewSet): - queryset = Post.objects.all().select_related( - "storage", - "product", - "author", - "product__brand", + queryset = ( + Post.objects.all() + .select_related("storage", "product", "author", "product__brand") + .prefetch_related("images") ) serializer_class = PostSerializer permission_classes = [IsAuthenticated, IsAuthorOrReadOnly]