Django Nestesd 序列化器的“子类别”问题对象不可迭代
我想使用我的产品序列化程序获取类别和子类别,但它向我显示子类别对象不可迭代的错误。我不知道问题是什么,我尝试了相同的嵌套过程,它之前适用于另一个字段,但不适用于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用模型序列化器作为字段来指定嵌套关系 。
您将
many=True
传递给SubcategorySerializer
,即使只有一个子类别,因此它应该为 FalseYou can use model serializers as fields to specify nested relationships.
You passed
many=True
to yourSubcategorySerializer
even though there is only one subcategory so it should be False