我正在尝试导出CSV in Django
并使用values_list
来选择我要导出的字段。
My First Try
class ExportCSV(APIView):
def get(self, request, *args, **kwargs):
incidents = Incident.objects.filter(
interview_date__range=(start, end),
partner__twg=self.request.query_params.get("twg"),
)
for incident in incidents:
writer.writerow(
[
incident.incidenttwg1.getchild.values_list( # <-- This line
"choice", flat=True
)
]
)
我懂了。<QuerySet ['Hello', 'Gudbye']>
,所以我决定创建loop
来获取Hello and Gudbye
。
Here my second Try
class ExportCSV(APIView):
def getincident(self, request, incident): # Create a function
for incident in incident.incidenttwg1.getchild.values_list("choice", flat=True):
return incident
def get(self, request, *args, **kwargs):
incidents = Incident.objects.filter(
interview_date__range=(start, end),
partner__twg=self.request.query_params.get("twg"),
)
for incident in incidents:
writer.writerow([self.getincident(request, incident)]) # Call function
我创建了一个getincident
函数来让它cleanable
读取。
我得到的是Hello
,它应该是Hello
,而Gudbye
不仅仅是Hello
。
有什么帮助吗??谢谢....