From ba4f4db4627f8836d6bba77a4a32b79887b9436a Mon Sep 17 00:00:00 2001 From: jhyns Date: Thu, 18 May 2023 10:03:47 +0900 Subject: [PATCH] Remove ProductColor --- market/admin.py | 4 +--- market/models.py | 19 +++++++------------ market/serializers.py | 14 +++----------- market/viewsets.py | 4 ++-- user/models.py | 2 +- 5 files changed, 14 insertions(+), 29 deletions(-) diff --git a/market/admin.py b/market/admin.py index 5f16242..bb224ea 100644 --- a/market/admin.py +++ b/market/admin.py @@ -1,11 +1,10 @@ from django.contrib import admin -from .models import Brand, Product, ProductColor, ProductStorage, Post, Image +from .models import Brand, Product, ProductStorage, Post, Image admin.site.register(Brand) admin.site.register(Product) -admin.site.register(ProductColor) admin.site.register(ProductStorage) admin.site.register(Image) @@ -25,5 +24,4 @@ class PostAdmin(admin.ModelAdmin): list_filter = ("status", "written_at") search_fields = ("product", "text") date_hierarchy = "written_at" - ordering = ("status", "written_at") raw_id_fields = ("author",) diff --git a/market/models.py b/market/models.py index c2dcacd..a94773b 100644 --- a/market/models.py +++ b/market/models.py @@ -5,19 +5,11 @@ from user.models import User class Brand(models.Model): name = models.CharField(max_length=20) - image = models.ImageField(upload_to="brands", blank=True, null=True) def __str__(self): return self.name -class ProductColor(models.Model): - color = models.CharField(max_length=20) - - def __str__(self): - return self.color - - class ProductStorage(models.Model): storage = models.CharField(max_length=20) @@ -30,14 +22,14 @@ class Product(models.Model): brand = models.ForeignKey( Brand, on_delete=models.CASCADE, related_name="products" ) - color = models.ManyToManyField( - "ProductColor", blank=True, related_name="products" - ) storage = models.ManyToManyField( "ProductStorage", blank=True, related_name="products" ) release_date = models.DateField(blank=True, null=True) + class Meta: + ordering = ["-release_date"] + def __str__(self): return self.name @@ -51,7 +43,7 @@ class Post(models.Model): product = models.ForeignKey( Product, on_delete=models.CASCADE, related_name="posts" ) - color = models.ForeignKey(ProductColor, on_delete=models.CASCADE) + color = models.CharField(max_length=20) storage = models.ForeignKey(ProductStorage, on_delete=models.CASCADE) price = models.IntegerField() text = models.TextField() @@ -61,6 +53,9 @@ class Post(models.Model): ) written_at = models.DateTimeField(auto_now_add=True) + class Meta: + ordering = ["-written_at"] + def __str__(self): return self.product.name diff --git a/market/serializers.py b/market/serializers.py index 84d35c9..f0dbfb7 100644 --- a/market/serializers.py +++ b/market/serializers.py @@ -4,7 +4,6 @@ from rest_framework.serializers import ModelSerializer from market.models import ( Brand, Product, - ProductColor, ProductStorage, Post, Image, @@ -17,12 +16,6 @@ class BrandSerializer(ModelSerializer): fields = "__all__" -class ProductColorSerializer(ModelSerializer): - class Meta: - model = ProductColor - fields = ("color",) - - class ProductStorageSerializer(ModelSerializer): class Meta: model = ProductStorage @@ -30,12 +23,11 @@ class ProductStorageSerializer(ModelSerializer): class ProductSerializer(ModelSerializer): - color = ProductColorSerializer(many=True, read_only=True) storage = ProductStorageSerializer(many=True, read_only=True) class Meta: model = Product - fields = ("id", "name", "brand", "color", "storage") + fields = ("id", "name", "brand", "storage") class ProductListSerializer(ModelSerializer): @@ -56,7 +48,7 @@ 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 @@ -64,7 +56,7 @@ class PostSerializer(ModelSerializer): "id", "product", "color", - "storage", + "storage__storage", "price", "text", "nickname", diff --git a/market/viewsets.py b/market/viewsets.py index 97d0667..710cedc 100644 --- a/market/viewsets.py +++ b/market/viewsets.py @@ -24,8 +24,8 @@ class BrandViewset(ActionBasedMixin, ModelViewSet): @action(detail=True, methods=["GET"]) def product(self, request, pk): - brand = self.get_object() - serializer = self.get_serializer(brand.products.all(), many=True) + queryset = self.get_object().products.all() + serializer = self.get_serializer(queryset, many=True) return Response(serializer.data) diff --git a/user/models.py b/user/models.py index 2a24691..8deebd9 100644 --- a/user/models.py +++ b/user/models.py @@ -3,7 +3,7 @@ from django.db import models class User(AbstractUser): - nickname = models.CharField(max_length=50) + nickname = models.CharField(max_length=10) # remove unnecessary fields first_name = None