HiPhone_BE/market/serializers.py

66 lines
1.4 KiB
Python

from rest_framework import serializers
from rest_framework.serializers import ModelSerializer
from market.models import (
Brand,
Product,
Post,
Image,
)
class ProductSerializer(ModelSerializer):
storage = serializers.StringRelatedField(many=True)
class Meta:
model = Product
fields = ("id", "name", "brand", "storage")
depth = 1
class ProductListSerializer(ModelSerializer):
class Meta:
model = Product
fields = ("id", "name")
class BrandSerializer(ModelSerializer):
products = ProductListSerializer(many=True, read_only=True)
class Meta:
model = Brand
fields = ["id", "name", "products"]
class BrandListSerializer(ModelSerializer):
class Meta:
model = Brand
fields = "__all__"
class ImageSerializer(ModelSerializer):
class Meta:
model = Image
fields = ("image",)
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")
class Meta:
model = Post
fields = (
"id",
"product",
"storage",
"price",
"text",
"nickname",
"status",
"written_at",
"images",
)