from rest_framework import serializers
from meituan.models import Merchant,Goods,GoodsCategory


class MerchantSerializer(serializers.ModelSerializer):
    class Meta:
        model = Merchant
        fields = "__all__"


class GoodsSerializer(serializers.ModelSerializer):

    class Meta:
        model = Goods
        fields = "__all__"

    def create(self, validated_data):

        return Goods.objects.create(**validated_data)


class GoodsCategorySerializer(serializers.ModelSerializer):
    # merchant = MerchantSerializer(read_only=True)
    merchant_id = serializers.IntegerField()
    goods_list = GoodsSerializer(many=True,read_only=True)
    class Meta:
        model = GoodsCategory
        # fields = "__all__"
        exclude = ['merchant']

    def validate_merchant_id(self,value):
        if not Merchant.objects.filter(pk=value).exists():
            raise serializers.ValidationError("商家不存在！")
        return value

    def create(self,validated_data):
        merchant_id = validated_data.get('merchant_id')
        merchant = Merchant.objects.get(pk=merchant_id)
        category = GoodsCategory.objects.create(**validated_data,merchant=merchant)
        return category