From d12bc6cf9903a364acf5c717c5a603b606d036e6 Mon Sep 17 00:00:00 2001 From: jhyns Date: Mon, 19 Jun 2023 03:56:23 +0900 Subject: [PATCH] Remove ProductStorage model in market app and add PostCreateSerializer --- market/admin.py | 4 +--- market/models.py | 11 ----------- market/serializers.py | 18 +++++++++++------- market/viewsets.py | 10 ++++------ 4 files changed, 16 insertions(+), 27 deletions(-) diff --git a/market/admin.py b/market/admin.py index d941b3d..0f6814b 100644 --- a/market/admin.py +++ b/market/admin.py @@ -1,11 +1,10 @@ from django.contrib import admin -from .models import Brand, Product, ProductStorage, Post, Image +from .models import Brand, Product, Post, Image admin.site.register(Brand) admin.site.register(Product) -admin.site.register(ProductStorage) admin.site.register(Image) @@ -13,7 +12,6 @@ admin.site.register(Image) class PostAdmin(admin.ModelAdmin): list_display = ( "product", - "storage", "price", "text", "author", diff --git a/market/models.py b/market/models.py index e846158..a4fbd15 100644 --- a/market/models.py +++ b/market/models.py @@ -10,21 +10,11 @@ class Brand(models.Model): return self.name -class ProductStorage(models.Model): - storage = models.CharField(max_length=20) - - def __str__(self): - return self.storage - - class Product(models.Model): name = models.CharField(max_length=50) brand = models.ForeignKey( Brand, on_delete=models.CASCADE, related_name="products" ) - storage = models.ManyToManyField( - "ProductStorage", blank=True, related_name="products" - ) release_date = models.DateField(blank=True, null=True) class Meta: @@ -43,7 +33,6 @@ class Post(models.Model): product = models.ForeignKey( Product, on_delete=models.CASCADE, related_name="posts" ) - storage = models.ForeignKey(ProductStorage, on_delete=models.CASCADE) price = models.IntegerField() text = models.TextField() author = models.ForeignKey(User, on_delete=models.SET_NULL, null=True) diff --git a/market/serializers.py b/market/serializers.py index 659ef78..0e98e18 100644 --- a/market/serializers.py +++ b/market/serializers.py @@ -10,11 +10,9 @@ from market.models import ( class ProductSerializer(ModelSerializer): - storage = serializers.StringRelatedField(many=True) - class Meta: model = Product - fields = ("id", "name", "brand", "storage") + fields = ("id", "name", "brand") depth = 1 @@ -48,14 +46,12 @@ class PostListSerializer(ModelSerializer): product = ProductListSerializer(read_only=True) nickname = serializers.CharField(source="author.nickname") image = serializers.SerializerMethodField() - storage = serializers.CharField(source="storage.storage") class Meta: model = Post fields = ( "id", "product", - "storage", "price", "text", "nickname", @@ -71,18 +67,26 @@ class PostListSerializer(ModelSerializer): ).data["image"] +class PostCreateSerializer(ModelSerializer): + class Meta: + model = Post + fields = ( + "product", + "price", + "text", + ) + + 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") class Meta: model = Post fields = ( "id", "product", - "storage", "price", "text", "nickname", diff --git a/market/viewsets.py b/market/viewsets.py index 07d37a3..b52c73d 100644 --- a/market/viewsets.py +++ b/market/viewsets.py @@ -10,6 +10,7 @@ from market.serializers import ( BrandSerializer, ProductSerializer, PostSerializer, + PostCreateSerializer, PostListSerializer, ) @@ -22,11 +23,7 @@ class BrandViewset(ActionBasedMixin, ModelViewSet): class ProductViewset(ActionBasedMixin, ModelViewSet): - queryset = ( - Product.objects.all() - .select_related("brand") - .prefetch_related("storage") - ) + queryset = Product.objects.all().select_related("brand") serializer_class = ProductSerializer serializer_class_map = { "posts": PostSerializer, @@ -50,12 +47,13 @@ class ProductViewset(ActionBasedMixin, ModelViewSet): class PostViewset(ActionBasedMixin, ModelViewSet): queryset = ( Post.objects.all() - .select_related("storage", "product", "author", "product__brand") + .select_related("product", "author", "product__brand") .prefetch_related("images") ) serializer_class = PostSerializer serializer_class_map = { "list": PostListSerializer, + "create": PostCreateSerializer, } permission_classes = [IsAuthenticated, IsAuthorOrReadOnly] permission_classes_map = {