Django CSV导出-Foreingkey
导出到CSV工作正常。。。但唯一的问题是,通过ForeignKey定义的模型字段被输出为它们的PK。。。我怎样才能解决这个问题 下面是my views.py中的代码:Django CSV导出-Foreingkey,django,csv,foreign-keys,export,Django,Csv,Foreign Keys,Export,导出到CSV工作正常。。。但唯一的问题是,通过ForeignKey定义的模型字段被输出为它们的PK。。。我怎样才能解决这个问题 下面是my views.py中的代码: def export_cashflow_csv(request): response = HttpResponse(content_type='text/csv') response['Content-Disposition'] = 'attachment; filename="cashflow.csv"' writer
def export_cashflow_csv(request):
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="cashflow.csv"'
writer = csv.writer(response)
writer.writerow(['date', 'type', 'amount', 'fund', 'description'])
cashflows = CashFlow.objects.all().values_list('date', 'type', 'amount', 'fund', 'description')
for cashflow in cashflows:
writer.writerow(cashflow)
return response
外地基金是示范基金的外键
非常感谢大家 您必须包括要导出的
基金
字段,例如
cashflows = CashFlow.objects.values_list(
'date',
'type',
'amount',
'fund__name',
'fund__type',
'description')
您必须包括要导出的
Fund
字段,例如
cashflows = CashFlow.objects.values_list(
'date',
'type',
'amount',
'fund__name',
'fund__type',
'description')