我是Django的新手,遇到了以下问题:我有一个模型FundPrice存储特定基金的每日价格。我想从数据库中检索它并传递给基于js的图表。
views.py
from django.shortcuts import render
from fundmgmt.models import FundPrice
def fund_details_cards(request):
fp = list(FundPrice.objects.values('fund_id','value_date','price'))
return render(request,"fundmgmt/fund_detail_cards.html",
{'fund_prices': fp})
在我的模板中,通过使用{{ fund_prices }}
引用,我得到的结果如下:
[{'fund_id': 1, 'value_date': datetime.date(2016, 11, 9), 'price': Decimal('2.574557')},
{'fund_id': 1, 'value_date': datetime.date(2016, 11, 8), 'price': Decimal('2.572507')},
{'fund_id': 1, 'value_date': datetime.date(2016, 11, 7), 'price': Decimal('2.571724')}]
我可以以某种方式格式化此输出吗?例如。摆脱这些datetime.date(
,Decimal(
字段类型指标?
我想要打印的内容如下:
[{'fund_id': 1, 'value_date': '2016-11-09', 'price': '2.574557'},
{'fund_id': 1, 'value_date': '2016-11-08', 'price': '2.572507'},
{'fund_id': 1, 'value_date': '2016-11-07', 'price': '2.571724'}]
答案 0 :(得分:0)
您可以简单地使用$actual_option_price = $this->pricingRepository->getOptionPrice($insertedRowID, 1, 100);
函数强制表达式的结果类型为output_field中的结果类型。
在您的情况下,您会将Cast
和value_date
字段投放到price
:
CharField