HiPhone_BE/market/serializers.py

75 lines
1.7 KiB
Python
Raw Normal View History

from rest_framework import serializers
from rest_framework.serializers import ModelSerializer
from market.models import (
Brand,
Product,
ProductColor,
ProductStorage,
Post,
Image,
)
class BrandSerializer(ModelSerializer):
class Meta:
model = Brand
fields = "__all__"
class ProductColorSerializer(ModelSerializer):
class Meta:
model = ProductColor
fields = "__all__"
class ProductStorageSerializer(ModelSerializer):
class Meta:
model = ProductStorage
fields = "__all__"
class ProductSerializer(ModelSerializer):
colors = ProductColorSerializer(many=True, read_only=True)
storages = ProductStorageSerializer(many=True, read_only=True)
class Meta:
model = Product
fields = ("id", "name", "brand", "colors", "storages")
2023-05-17 16:10:25 +09:00
class ProductListSerializer(ModelSerializer):
brand = serializers.CharField(source="brand.name")
class Meta:
model = Product
fields = ("id", "name", "brand")
class ImageSerializer(ModelSerializer):
class Meta:
model = Image
fields = ("image",)
class PostSerializer(ModelSerializer):
2023-05-17 16:10:25 +09:00
product = ProductListSerializer(read_only=True)
nickname = serializers.CharField(source="author.nickname")
images = ImageSerializer(many=True, read_only=True)
2023-05-17 16:10:25 +09:00
storage = serializers.CharField(source="storage.storage")
class Meta:
model = Post
fields = (
"id",
"product",
"color",
"storage",
"price",
"text",
"nickname",
"status",
"written_at",
"images",
)