[Answered ]-Django nestesd serializers problem for 'Subcategory' object is not iterable

1👍

You can use model serializers as fields to specify nested relationships.

You passed many=True to your SubcategorySerializer even though there is only one subcategory so it should be False

class CategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = Category
        fields = '__all__'


class SubcategorySerializer(serializers.ModelSerializer):
    category = CategorySerializer(read_only=True)

    class Meta:
        model = Subcategory
        fields = '__all__'


class ProductSerializer(serializers.ModelSerializer):
    user = VendorSerializer(read_only=True)
    subcategory = SubcategorySerializer(read_only=True, required=False)

    class Meta:
        model = Product
        fields = '__all__'

Leave a comment