无法使用ListAPI View Django Rest Framework获取嵌套的模型数据

时间:2018-09-22 06:21:35

标签: python django django-rest-framework

我有一个django rest框架应用程序,它基于模型创建项目结构。一个项目可以有多个部分,即部分1,部分2等,并且每个部分只能有一个部分信息和部分问题。为了避免在不同的节表中创建相同的列,我创建了一个单独的sectionInfo和sectionQuestion模型,该模型通过外键链接到项目。 SectionQuestion和section1,section2通过onetoonefield链接到sectionInfo。

问题是我想以项目序列化程序中定义的格式获取详细信息,其中section1和section2会附带项目详细信息,而我希望信息和问题详细信息作为section1,section2的一部分作为json数据。

我的section1,section2和sectionquestion通过sectionInfo模型连接到项目模型,我希望反向输出,如下所示:-

{
            "p_name": "test2",
            "p_name_full": "test2-2018-09-20",
            "section1": {
                         .......
                         "sectionInfo":{.........}
                         "sectionQues":{.........}
                        },
            "section2": {
                         .......
                         "sectionInfo":{.........}
                         "sectionQues":{.........}
                        }
}

views.py

class ProjectListAPIView(ListAPIView):            
    permission_classes = [AllowAny]
    serializer_class = ProjectSerializer

    def get_queryset(self):
        return Projects.objects.all()

    def list(self, request):
        queryset = self.get_queryset()
        serializer = self.get_serializer(queryset, many=True)        
        if not serializer.data:
            return Response({'code': 'empty'}, status=HTTP_200_OK)
        return Response({'code': 'retrieve_success', 'message': serializer.data}, status=HTTP_200_OK)

models.py

class Projects(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL,
                             on_delete=models.CASCADE)
    p_name = models.CharField(max_length=100)
    p_name_full = models.CharField(max_length=150)

class SectionInfo(models.Model):
    p_id = models.ForeignKey(Projects, 
        on_delete=models.CASCADE, related_name='sec_proj', db_column='p_id')
    s_name = models.CharField(max_length=200)
    s_type = models.CharField(max_length=200, db_column='type')
    s_length = models.IntegerField(null=True, blank=True)

class SectionQuestion(models.Model):
    info_id = models.OneToOneField('rsa.SectionInfo', 
        on_delete=models.CASCADE, related_name='sec_ques', db_column='info_id')
    s_q1r = models.TextField(max_length=10000,null=True, blank=True)
    s_q2r = models.TextField(max_length=10000,null=True, blank=True)

class Section1(models.Model):
    info_id = models.OneToOneField('rsa.SectionInfo', 
        on_delete=models.CASCADE, related_name='sec_1_info', db_column='info_id')
    s_cbRestrictionEnd_1 = models.IntegerField(null=True, blank=True)
    s_cbCone_1 = models.IntegerField(null=True, blank=True)
    s_cbWorkTraff_1 = models.IntegerField(null=True, blank=True)
    s_cbReflector_1 = models.IntegerField(null=True, blank=True)

class Section2(models.Model):
    info_id = models.OneToOneField('rsa.SectionInfo', 
        on_delete=models.CASCADE, related_name='sec_2_info', db_column='info_id')
    s_cbRestrictionEnd_1 = models.IntegerField(null=True, blank=True)
    s_cbReflector_1 = models.IntegerField(null=True, blank=True)
    s_cbmReflector_1 = models.IntegerField(null=True, blank=True)
    s_cbDiversionRight_1 = models.IntegerField(null=True, blank=True)

serializers.py

class ProjectSerializer(ModelSerializer):
    sec_1 = Section1Serializer(many=True, required=False, read_only=False)
    sec_2 = Section2Serializer(many=True, required=False, read_only=False)

    class Meta:
        depth = 2
        model = Projects
        fields = [
            'sec_1',
            'sec_2',
        ]



class SectionInfoSerializer(ModelSerializer):

    class Meta:
        depth = 2
        model = SectionInfo
        fields = [
            's_name',
            's_type',
            's_length',
        ]

class SectionQuestionSerializer(ModelSerializer):
    class Meta:
        model = SectionQuestion
        fields = [
            's_q1r',
            's_q2r',


class Section1Serializer(ModelSerializer):
    sec_info = SectionInfoSerializer(required=False)
    sec_ques = SectionQuestionSerializer(required=False)

    class Meta:
        model = Section1
        fields = [
            'sec_info',
            'sec_ques',
            's_cbRestrictionEnd_1',
            's_cbCone_1',
            's_cbWorkTraff_1',
            's_cbReflector_1',
        ]


class Section2Serializer(ModelSerializer):
    sec_info = SectionInfoSerializer(required=False)
    sec_ques = SectionQuestionSerializer(required=False)

    class Meta:
        model = Section2
        fields = [
            'sec_info',
            'sec_ques',
            's_cbRestrictionEnd_1',
            's_cbReflector_1',
            's_cbmReflector_1',
            's_cbDiversionRight_1',
        ]

0 个答案:

没有答案