在Django-rest-framework中加入表字段

时间:2014-11-22 14:58:36

标签: python django django-models django-rest-framework

我正在进入PHP到Django的迁移项目,我无法更改数据库的模式

对于没有FK的字段,有些地方存在多个连接,我现在无权添加它们。即使在我的Django模型上,由于数据库的庞大规模和项目交付的时间限制。

我正在使用Django-rest-framework并重写list方法和queryset来接受这些原始sqls。

sql = """SELECT
  a.field1,
  b.field2
FROM a
LEFT JOIN b ON a.fieldx = b.fieldy"""

问题是我无法在字段元组内的序列化器类中添加连接表的字段 因为我说模型= models.A而且这个模型没有关于模型B的信息。

我们将不胜感激。

谢谢!

UPDATE 我只使用viewset。我没有使用任何视图(这是错的吗?) 我的观点

class AdminclassinfoViewSet(viewsets.ModelViewSet): 
    queryset = models.Classroominfo.objects.all()
    serializer_class = ClassroominfoSerializer

    def list(self, request):
       sql = """
         SELECT -- DISTINCT 
           cri.classroomid,
           cri.assessmentid, 
           cri.resourceid,
           al.assessmenttype,
           al.assessmenttitle,
           ri.resourcetype,
           ri.resourcetitle,
           wwi.writtenworktitle,
           cri.writtenworkid, 
           si.firstname,
           si.imageurl,
           date(cri.posteddate) as posteddate,
           cri.studentid
       FROM classroominfo cri
       LEFT OUTER JOIN assignassessmentinfo aai ON aai.assessmentid = cri.assessmentid 
                   AND aai.studentid = cri.studentid 
       LEFT OUTER JOIN assessmentlist al ON al.assessmentid = cri.assessmentid 
       LEFT OUTER JOIN assignresourceinfo ari  ON ari.resourceid = cri.resourceid 
                  AND ari.studentid = cri.studentid 
       LEFT OUTER JOIN resourceinfo ri ON ri.resourceid = cri.resourceid
       LEFT OUTER JOIN assignwrittenworkinfo awwi ON awwi.writtenworkid = cri.writtenworkid
       LEFT OUTER JOIN writtenworkinfo wwi ON wwi.writtenworkid = cri.writtenworkid
       LEFT OUTER JOIN logininfo li ON li.loginid = cri.studentid
       LEFT OUTER JOIN studentinfo si ON si.username = li.username
       WHERE ( ari.isclassroom =1 OR 
             aai.isclassroom =1 OR 
            awwi.isclassroom=1 ) 
       ORDER BY cri.classroomid DESC
       """
       queryset = models.Classroominfo.objects.raw(sql)
       serializer_class = ClassroominfoSerializer
       serializer = ClassroominfoSerializer(queryset, many=True)        
       return Response(serializer.data)

我的序列化程序。我不知道如何在这里引用加入的字段......

class ClassroominfoSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Classroominfo
        fields = ('classroomid',  'assessmentid','resourceid',
              'writtenworkid')

2 个答案:

答案 0 :(得分:1)

由于我处于截止日期,我恢复运行queries directly并转换为dicts列表并从我的listset方法组返回,如下所示:

desc = cursor.description
result =  [
            dict(zip([col[0] for col in desc], row))
            for row in cursor.fetchall()
    ]
return Response(result)

它仍然使用Django Rest API接口,因此对我来说,偶尔使用硬编码的sql查询并不是什么大问题。

但是,如果Django Rest Framework 3.x可以灵活处理这种用例,我将不胜感激。

答案 1 :(得分:0)

因此,根据您的SQL查询,似乎classroominfo表位于某种星型模式的中心。您应该可以使用“深度”属性来包含nested serializations

class ClassroominfoSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Classroominfo
        fields = ('classroomid',  'assessmentid','resourceid', 'writtenworkid')
        depth = 2