Django CSV 导出 - Foreingkey

TMD

导出到 CSV 工作正常......但唯一的问题是通过 ForeignKey 定义的模型字段作为它们的 PK 输出......我该如何解决这个问题?

在我的 views.py 中的代码下面:

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

现场基金是模型基金的外键。

非常感谢大家!

两个

您必须包含Fund要导出字段,例如

cashflows = CashFlow.objects.values_list(
    'date', 
    'type', 
    'amount', 
    'fund__name', 
    'fund__type', 
    'description')

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章