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 .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",)

View File

@ -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

View File

@ -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",

View File

@ -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)

View File

@ -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