Remove ProductStorage model in market app and add PostCreateSerializer

This commit is contained in:
jhyns 2023-06-19 03:56:23 +09:00
parent 7700664f81
commit d12bc6cf99
4 changed files with 16 additions and 27 deletions

View File

@ -1,11 +1,10 @@
from django.contrib import admin 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(Brand)
admin.site.register(Product) admin.site.register(Product)
admin.site.register(ProductStorage)
admin.site.register(Image) admin.site.register(Image)
@ -13,7 +12,6 @@ admin.site.register(Image)
class PostAdmin(admin.ModelAdmin): class PostAdmin(admin.ModelAdmin):
list_display = ( list_display = (
"product", "product",
"storage",
"price", "price",
"text", "text",
"author", "author",

View File

@ -10,21 +10,11 @@ class Brand(models.Model):
return self.name return self.name
class ProductStorage(models.Model):
storage = models.CharField(max_length=20)
def __str__(self):
return self.storage
class Product(models.Model): class Product(models.Model):
name = models.CharField(max_length=50) name = models.CharField(max_length=50)
brand = models.ForeignKey( brand = models.ForeignKey(
Brand, on_delete=models.CASCADE, related_name="products" 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) release_date = models.DateField(blank=True, null=True)
class Meta: class Meta:
@ -43,7 +33,6 @@ 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"
) )
storage = models.ForeignKey(ProductStorage, on_delete=models.CASCADE)
price = models.IntegerField() price = models.IntegerField()
text = models.TextField() text = models.TextField()
author = models.ForeignKey(User, on_delete=models.SET_NULL, null=True) author = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)

View File

@ -10,11 +10,9 @@ from market.models import (
class ProductSerializer(ModelSerializer): class ProductSerializer(ModelSerializer):
storage = serializers.StringRelatedField(many=True)
class Meta: class Meta:
model = Product model = Product
fields = ("id", "name", "brand", "storage") fields = ("id", "name", "brand")
depth = 1 depth = 1
@ -48,14 +46,12 @@ class PostListSerializer(ModelSerializer):
product = ProductListSerializer(read_only=True) product = ProductListSerializer(read_only=True)
nickname = serializers.CharField(source="author.nickname") nickname = serializers.CharField(source="author.nickname")
image = serializers.SerializerMethodField() image = serializers.SerializerMethodField()
storage = serializers.CharField(source="storage.storage")
class Meta: class Meta:
model = Post model = Post
fields = ( fields = (
"id", "id",
"product", "product",
"storage",
"price", "price",
"text", "text",
"nickname", "nickname",
@ -71,18 +67,26 @@ class PostListSerializer(ModelSerializer):
).data["image"] ).data["image"]
class PostCreateSerializer(ModelSerializer):
class Meta:
model = Post
fields = (
"product",
"price",
"text",
)
class PostSerializer(ModelSerializer): 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")
class Meta: class Meta:
model = Post model = Post
fields = ( fields = (
"id", "id",
"product", "product",
"storage",
"price", "price",
"text", "text",
"nickname", "nickname",

View File

@ -10,6 +10,7 @@ from market.serializers import (
BrandSerializer, BrandSerializer,
ProductSerializer, ProductSerializer,
PostSerializer, PostSerializer,
PostCreateSerializer,
PostListSerializer, PostListSerializer,
) )
@ -22,11 +23,7 @@ class BrandViewset(ActionBasedMixin, ModelViewSet):
class ProductViewset(ActionBasedMixin, ModelViewSet): class ProductViewset(ActionBasedMixin, ModelViewSet):
queryset = ( queryset = Product.objects.all().select_related("brand")
Product.objects.all()
.select_related("brand")
.prefetch_related("storage")
)
serializer_class = ProductSerializer serializer_class = ProductSerializer
serializer_class_map = { serializer_class_map = {
"posts": PostSerializer, "posts": PostSerializer,
@ -50,12 +47,13 @@ class ProductViewset(ActionBasedMixin, ModelViewSet):
class PostViewset(ActionBasedMixin, ModelViewSet): class PostViewset(ActionBasedMixin, ModelViewSet):
queryset = ( queryset = (
Post.objects.all() Post.objects.all()
.select_related("storage", "product", "author", "product__brand") .select_related("product", "author", "product__brand")
.prefetch_related("images") .prefetch_related("images")
) )
serializer_class = PostSerializer serializer_class = PostSerializer
serializer_class_map = { serializer_class_map = {
"list": PostListSerializer, "list": PostListSerializer,
"create": PostCreateSerializer,
} }
permission_classes = [IsAuthenticated, IsAuthorOrReadOnly] permission_classes = [IsAuthenticated, IsAuthorOrReadOnly]
permission_classes_map = { permission_classes_map = {