Django嵌套一对多序列化器问题:无法获取三级分类嵌套数据
数据库分类关联嵌套结构问题
我正在构建数据库间的关联关系,当前接口仅返回二级分类数据,无法展示三级分类的嵌套结构。
当前返回结果
[ { "id":1, "title":"mobile", "category_two":[ { "id":3, "title":"brand" }, { "id":4, "title":"memory" } ] } ]
期望返回结果
[ { "id":1, "title":"mobile", "category_two":[ { "id":3, "title":"brand", "category_three":[ { "id":1, "title":"samsung" }, { "id":2, "title":"apple" } ] }, { "id":4, "title":"memory", "category_three":[ { "id":1, "title":"32gb" }, { "id":2, "title":"64gb" } ] } ] } ]
现有代码实现
views
class get_Category(APIView): def get(self, request): category = CategoryOne.objects.all() serializer = CategoryTwoSerializer(category, many=True) return Response(serializer.data)
serializers
class CategoryOneSerializer(serializers.ModelSerializer): class Meta: model = CategoryOne fields = '__all__' class CategoryTwoSerializer(serializers.ModelSerializer): category_two= CategoryOneSerializer(many=True,read_only=True) class Meta: model = CategoryTwo fields = '__all__' depth=5 class CategoryThreeSerializer(serializers.ModelSerializer): category_three = CategoryTwoSerializer(many=True,read_only=True) class Meta: model = CategoryThree fields = '__all__' depth=5
models
class CategoryOne(models.Model): title = models.CharField(max_length=225) def __str__(self): return self.title class CategoryTwo(models.Model): title = models.CharField(max_length=255) categoryone = models.ForeignKey(CategoryOne,related_name='category_two',on_delete=models.SET_NULL,null=True) def __str__(self): return self.title class CategoryThree(models.Model): title = models.CharField(max_length=255) categorytwo = models.ForeignKey(CategoryTwo,related_name='category_three',on_delete=models.SET_NULL,null=True) def __str__(self): return self.title
解决方案
你的代码存在几个关键问题,导致无法返回三级分类嵌套结构:
序列化器关联逻辑错误:
CategoryTwoSerializer错误关联了CategoryOneSerializer,但CategoryOne通过related_name='category_two'关联的是CategoryTwo实例,应该在CategoryOneSerializer中嵌套CategoryTwoSerializer。CategoryThreeSerializer错误关联了CategoryTwoSerializer,正确逻辑是在CategoryTwoSerializer中嵌套CategoryThreeSerializer,对应CategoryTwo的related_name='category_three'。
视图使用了错误的序列化器:
- 视图查询
CategoryOne数据,却用了CategoryTwoSerializer,应该使用CategoryOneSerializer来序列化一级分类及其关联的二级、三级分类。
- 视图查询
不必要的
depth参数:- 手动嵌套序列化器时无需设置
depth,否则会导致序列化逻辑冲突。
- 手动嵌套序列化器时无需设置
修改后的代码如下:
修改后的serializers
class CategoryThreeSerializer(serializers.ModelSerializer): class Meta: model = CategoryThree fields = '__all__' class CategoryTwoSerializer(serializers.ModelSerializer): # 关联三级分类 category_three = CategoryThreeSerializer(many=True, read_only=True) class Meta: model = CategoryTwo fields = '__all__' class CategoryOneSerializer(serializers.ModelSerializer): # 关联二级分类 category_two = CategoryTwoSerializer(many=True, read_only=True) class Meta: model = CategoryOne fields = '__all__'
修改后的views
class get_Category(APIView): def get(self, request): category = CategoryOne.objects.all() # 使用正确的序列化器 serializer = CategoryOneSerializer(category, many=True) return Response(serializer.data)
修改后接口即可返回包含三级分类的嵌套结构,与期望结果一致。
内容的提问来源于stack exchange,提问作者zor
相关产品推荐
相关产品推荐

