75 lines
1.7 KiB
Python
75 lines
1.7 KiB
Python
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")
|
|
|
|
|
|
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):
|
|
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",
|
|
"color",
|
|
"storage",
|
|
"price",
|
|
"text",
|
|
"nickname",
|
|
"status",
|
|
"written_at",
|
|
"images",
|
|
)
|