Remove ProductColor

This commit is contained in:
jhyns 2023-05-18 10:03:47 +09:00
parent 471ce05898
commit ba4f4db462
5 changed files with 14 additions and 29 deletions

View File

@ -1,11 +1,10 @@
from django.contrib import admin 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(Brand)
admin.site.register(Product) admin.site.register(Product)
admin.site.register(ProductColor)
admin.site.register(ProductStorage) admin.site.register(ProductStorage)
admin.site.register(Image) admin.site.register(Image)
@ -25,5 +24,4 @@ class PostAdmin(admin.ModelAdmin):
list_filter = ("status", "written_at") list_filter = ("status", "written_at")
search_fields = ("product", "text") search_fields = ("product", "text")
date_hierarchy = "written_at" date_hierarchy = "written_at"
ordering = ("status", "written_at")
raw_id_fields = ("author",) raw_id_fields = ("author",)

View File

@ -5,19 +5,11 @@ from user.models import User
class Brand(models.Model): class Brand(models.Model):
name = models.CharField(max_length=20) name = models.CharField(max_length=20)
image = models.ImageField(upload_to="brands", blank=True, null=True)
def __str__(self): def __str__(self):
return self.name return self.name
class ProductColor(models.Model):
color = models.CharField(max_length=20)
def __str__(self):
return self.color
class ProductStorage(models.Model): class ProductStorage(models.Model):
storage = models.CharField(max_length=20) storage = models.CharField(max_length=20)
@ -30,14 +22,14 @@ class Product(models.Model):
brand = models.ForeignKey( brand = models.ForeignKey(
Brand, on_delete=models.CASCADE, related_name="products" Brand, on_delete=models.CASCADE, related_name="products"
) )
color = models.ManyToManyField(
"ProductColor", blank=True, related_name="products"
)
storage = models.ManyToManyField( storage = models.ManyToManyField(
"ProductStorage", blank=True, related_name="products" "ProductStorage", blank=True, related_name="products"
) )
release_date = models.DateField(blank=True, null=True) release_date = models.DateField(blank=True, null=True)
class Meta:
ordering = ["-release_date"]
def __str__(self): def __str__(self):
return self.name return self.name
@ -51,7 +43,7 @@ class Post(models.Model):
product = models.ForeignKey( product = models.ForeignKey(
Product, on_delete=models.CASCADE, related_name="posts" 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) storage = models.ForeignKey(ProductStorage, on_delete=models.CASCADE)
price = models.IntegerField() price = models.IntegerField()
text = models.TextField() text = models.TextField()
@ -61,6 +53,9 @@ class Post(models.Model):
) )
written_at = models.DateTimeField(auto_now_add=True) written_at = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ["-written_at"]
def __str__(self): def __str__(self):
return self.product.name return self.product.name

View File

@ -4,7 +4,6 @@ from rest_framework.serializers import ModelSerializer
from market.models import ( from market.models import (
Brand, Brand,
Product, Product,
ProductColor,
ProductStorage, ProductStorage,
Post, Post,
Image, Image,
@ -17,12 +16,6 @@ class BrandSerializer(ModelSerializer):
fields = "__all__" fields = "__all__"
class ProductColorSerializer(ModelSerializer):
class Meta:
model = ProductColor
fields = ("color",)
class ProductStorageSerializer(ModelSerializer): class ProductStorageSerializer(ModelSerializer):
class Meta: class Meta:
model = ProductStorage model = ProductStorage
@ -30,12 +23,11 @@ class ProductStorageSerializer(ModelSerializer):
class ProductSerializer(ModelSerializer): class ProductSerializer(ModelSerializer):
color = ProductColorSerializer(many=True, read_only=True)
storage = ProductStorageSerializer(many=True, read_only=True) storage = ProductStorageSerializer(many=True, read_only=True)
class Meta: class Meta:
model = Product model = Product
fields = ("id", "name", "brand", "color", "storage") fields = ("id", "name", "brand", "storage")
class ProductListSerializer(ModelSerializer): class ProductListSerializer(ModelSerializer):
@ -56,7 +48,7 @@ class PostSerializer(ModelSerializer):
product = ProductListSerializer(read_only=True) product = ProductListSerializer(read_only=True)
nickname = serializers.CharField(source="author.nickname") nickname = serializers.CharField(source="author.nickname")
images = ImageSerializer(many=True, read_only=True) images = ImageSerializer(many=True, read_only=True)
storage = serializers.CharField(source="storage.storage") # storage = serializers.CharField(source="storage.storage")
class Meta: class Meta:
model = Post model = Post
@ -64,7 +56,7 @@ class PostSerializer(ModelSerializer):
"id", "id",
"product", "product",
"color", "color",
"storage", "storage__storage",
"price", "price",
"text", "text",
"nickname", "nickname",

View File

@ -24,8 +24,8 @@ class BrandViewset(ActionBasedMixin, ModelViewSet):
@action(detail=True, methods=["GET"]) @action(detail=True, methods=["GET"])
def product(self, request, pk): def product(self, request, pk):
brand = self.get_object() queryset = self.get_object().products.all()
serializer = self.get_serializer(brand.products.all(), many=True) serializer = self.get_serializer(queryset, many=True)
return Response(serializer.data) return Response(serializer.data)

View File

@ -3,7 +3,7 @@ from django.db import models
class User(AbstractUser): class User(AbstractUser):
nickname = models.CharField(max_length=50) nickname = models.CharField(max_length=10)
# remove unnecessary fields # remove unnecessary fields
first_name = None first_name = None