我到一個PHP到Django的遷移項目,在這裏我不能改變我的數據庫的模式連接表的字段
有在多個連接在那裏爲字段,而FK的地方,我有現在沒有權利添加它們。即使在我的Django模型中,也是因爲數據庫的大小和項目交付的時間限制。
我正在使用Django-rest-framework並重寫列表方法和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')
你可以包括你目前的序列化和視圖?甚至可能是模型取決於他們有多相關? – 2014-11-22 15:49:47
@Kevin更新了我的問題 – Jega 2014-11-22 18:33:15