# -*- coding: utf-8 -*-
from rest_framework import serializers
from .models import Goods, GoodsCategory


# class GoodsSerializer(serializers.Serializer):
#     name = serializers.CharField(required=False, max_length=100)
#     click_num = serializers.IntegerField(default=0)
#     # 当使用ImageField或者是文件类型时，drf会判断到是媒体类型，所以回去查找setting.py中的MEDIA路径，并加到路径头部
#     goods_front_image = serializers.ImageField()
#
#     def create(self, validated_data):
#         """
#         Create and return a new `Snippet` instance, given the validated data.
#         """
#         return Goods.objects.create(**validated_data)


class CategorySerializer3(serializers.ModelSerializer):

    class Meta:
        model = GoodsCategory
        fields = '__all__'


class CategorySerializer2(serializers.ModelSerializer):
    sub_cat = CategorySerializer3(many=True)

    class Meta:
        model = GoodsCategory
        fields = '__all__'


class CategorySerializer(serializers.ModelSerializer):
    sub_cat = CategorySerializer2(many=True)

    class Meta:
        model = GoodsCategory
        fields = '__all__'


class GoodsSerializer(serializers.ModelSerializer):
    category = CategorySerializer()

    class Meta:
        model = Goods
        # fields = ('name', 'click_num', 'market_price', 'add_time')
        fields = '__all__'

# -*- coding: utf-8 -*-
from rest_framework import serializers
from .models import Goods, GoodsCategory


# class GoodsSerializer(serializers.Serializer):
#     name = serializers.CharField(required=False, max_length=100)
#     click_num = serializers.IntegerField(default=0)
#     # 当使用ImageField或者是文件类型时，drf会判断到是媒体类型，所以回去查找setting.py中的MEDIA路径，并加到路径头部
#     goods_front_image = serializers.ImageField()
#
#     def create(self, validated_data):
#         """
#         Create and return a new `Snippet` instance, given the validated data.
#         """
#         return Goods.objects.create(**validated_data)


class CategorySerializer3(serializers.ModelSerializer):

    class Meta:
        model = GoodsCategory
        fields = '__all__'


class CategorySerializer2(serializers.ModelSerializer):
    sub_cat = CategorySerializer3(many=True)

    class Meta:
        model = GoodsCategory
        fields = '__all__'


class CategorySerializer(serializers.ModelSerializer):
    sub_cat = CategorySerializer2(many=True)

    class Meta:
        model = GoodsCategory
        fields = '__all__'


class GoodsSerializer(serializers.ModelSerializer):
    category = CategorySerializer()

    class Meta:
        model = Goods
        # fields = ('name', 'click_num', 'market_price', 'add_time')
        fields = '__all__'

# -*- coding: utf-8 -*- # class GoodsSerializer(serializers.Serializer): #     name = serializers.CharField(required=False, max_length=100) #     click_num = serializers.IntegerField(default=0) #     # 当使用ImageField或者是文件类型时，drf会判断到是媒体类型，所以回去查找setting.py中的MEDIA路径，并加到路径头部 #     goods_front_image = serializers.ImageField() # #     def create(self, validated_data): #         """ #         Create and return a new `Snippet` instance, given the validated data. #         """ #         return Goods.objects.create(**validated_data) # fields = ('name', 'click_num', 'market_price', 'add_time')