65 lines
1.4 KiB
Python
65 lines
1.4 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 ImageSerializer(ModelSerializer):
|
||
|
class Meta:
|
||
|
model = Image
|
||
|
fields = ("image",)
|
||
|
|
||
|
|
||
|
class PostSerializer(ModelSerializer):
|
||
|
nickname = serializers.CharField(source="author.nickname")
|
||
|
images = ImageSerializer(many=True, read_only=True)
|
||
|
|
||
|
class Meta:
|
||
|
model = Post
|
||
|
fields = (
|
||
|
"id",
|
||
|
"product",
|
||
|
"color",
|
||
|
"storage",
|
||
|
"price",
|
||
|
"text",
|
||
|
"nickname",
|
||
|
"status",
|
||
|
"written_at",
|
||
|
"images",
|
||
|
)
|