Django Nestesd 序列化器的“子类别”问题对象不可迭代

发布于 2025-01-09 17:29:40 字数 3274 浏览 0 评论 0原文

我想使用我的产品序列化程序获取类别和子类别,但它向我显示子类别对象不可迭代的错误。我不知道问题是什么,我尝试了相同的嵌套过程,它之前适用于另一个字段,但不适用于 subcategory 。

#这是我的模型,所以你了解关系

class Category(models.Model):
    name = models.CharField(max_length=220)

    def __str__(self):
        return self.name



class Subcategory(models.Model):
    category = models.ForeignKey(Category, on_delete=models.CASCADE)
    name = models.CharField(max_length=220)





class Product(models.Model):

    product_type_choices = [
        ('With Cylinder', 'With Cylinder'),
        ('Without Cylinder', 'Without Cylinder'),
    ]

    setup_type_choices = [
        ('Yes', 'Yes'),
        ('No', 'No'),
    ]

    user = models.ForeignKey(Vendor, on_delete=models.CASCADE)
    name = models.CharField(max_length=220)
    image = models.ImageField(null=True, blank=True)
    product_type = models.CharField(max_length=220, null=True, blank=True, choices=product_type_choices)
    setup_type = models.CharField(max_length=220, null=True, blank=True, choices=setup_type_choices)
    subcategory = models.ForeignKey(Subcategory, on_delete=models.CASCADE, null=True, blank=True)
    description = models.TextField(max_length=10000)
    rating = models.DecimalField(max_digits=7, decimal_places=2, blank=True, null=True)
    numReviews = models.IntegerField(null=True, blank=True, default=0)
    old_price = models.DecimalField(max_digits=11, decimal_places=2)
    discount = models.IntegerField(blank=True, null=True)
    price = models.DecimalField(max_digits=12, decimal_places=2, blank=True, null=True)
    countInStock = models.IntegerField(blank=True, null=True, default=0)
    createdAt = models.DateTimeField(auto_now_add=True)
    short_description = models.CharField(max_length=2000, blank=True, null=True)
    isCampaign = models.BooleanField(blank=True, null=True, default=False)
    _id = models.AutoField(primary_key=True, editable=False)
    

    def save(self, *args, **kwargs):
        self.price = Decimal(self.old_price * (100 - self.discount) / 100)
        return super(Product, self).save(*args, **kwargs)
    

    class Meta:
        ordering = ['-createdAt']


    def __str__(self):
        return self.name

#这是我的序列化器

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


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

    class Meta:
        model = Subcategory
        fields = '__all__'

    def get_category(self, obj):
        category = obj.category
        serializer = CategorySerializer(category, many=True)
        return serializer.data



class ProductSerializer(serializers.ModelSerializer):
    user = serializers.SerializerMethodField(read_only=True)
    subcategory = serializers.SerializerMethodField(read_only=True)

    class Meta:
        model = Product
        fields = '__all__'

    def get_user(self, obj):
        user = obj.user
        serializer = VendorSerializer(user, many=False)
        return serializer.data

    def get_subcategory(self, obj):
        subcategory = obj.subcategory
        serializer = SubcategorySerializer(subcategory, many=True)
        return serializer.data

I want to get the category and subcategory with my product serializer, but it is showing me this error that subcategory object is not iterable. I don't know what is the problem I tried the same nested procedure and it worked previously for another field but not with subcategory .

#this is my model so you understand the relation

class Category(models.Model):
    name = models.CharField(max_length=220)

    def __str__(self):
        return self.name



class Subcategory(models.Model):
    category = models.ForeignKey(Category, on_delete=models.CASCADE)
    name = models.CharField(max_length=220)





class Product(models.Model):

    product_type_choices = [
        ('With Cylinder', 'With Cylinder'),
        ('Without Cylinder', 'Without Cylinder'),
    ]

    setup_type_choices = [
        ('Yes', 'Yes'),
        ('No', 'No'),
    ]

    user = models.ForeignKey(Vendor, on_delete=models.CASCADE)
    name = models.CharField(max_length=220)
    image = models.ImageField(null=True, blank=True)
    product_type = models.CharField(max_length=220, null=True, blank=True, choices=product_type_choices)
    setup_type = models.CharField(max_length=220, null=True, blank=True, choices=setup_type_choices)
    subcategory = models.ForeignKey(Subcategory, on_delete=models.CASCADE, null=True, blank=True)
    description = models.TextField(max_length=10000)
    rating = models.DecimalField(max_digits=7, decimal_places=2, blank=True, null=True)
    numReviews = models.IntegerField(null=True, blank=True, default=0)
    old_price = models.DecimalField(max_digits=11, decimal_places=2)
    discount = models.IntegerField(blank=True, null=True)
    price = models.DecimalField(max_digits=12, decimal_places=2, blank=True, null=True)
    countInStock = models.IntegerField(blank=True, null=True, default=0)
    createdAt = models.DateTimeField(auto_now_add=True)
    short_description = models.CharField(max_length=2000, blank=True, null=True)
    isCampaign = models.BooleanField(blank=True, null=True, default=False)
    _id = models.AutoField(primary_key=True, editable=False)
    

    def save(self, *args, **kwargs):
        self.price = Decimal(self.old_price * (100 - self.discount) / 100)
        return super(Product, self).save(*args, **kwargs)
    

    class Meta:
        ordering = ['-createdAt']


    def __str__(self):
        return self.name

#this is my serializer

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


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

    class Meta:
        model = Subcategory
        fields = '__all__'

    def get_category(self, obj):
        category = obj.category
        serializer = CategorySerializer(category, many=True)
        return serializer.data



class ProductSerializer(serializers.ModelSerializer):
    user = serializers.SerializerMethodField(read_only=True)
    subcategory = serializers.SerializerMethodField(read_only=True)

    class Meta:
        model = Product
        fields = '__all__'

    def get_user(self, obj):
        user = obj.user
        serializer = VendorSerializer(user, many=False)
        return serializer.data

    def get_subcategory(self, obj):
        subcategory = obj.subcategory
        serializer = SubcategorySerializer(subcategory, many=True)
        return serializer.data

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

み格子的夏天 2025-01-16 17:29:40

您可以使用模型序列化器作为字段来指定嵌套关系

您将 many=True 传递给 SubcategorySerializer,即使只有一个子类别,因此它应该为 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__'

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__'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文