Reputation: 927
I am still new to Django/web development but have experience with Python and PSQL.
I have a psql database with a table that has an id, date, and value for a stock portfolio. The table is titled absolutedollarvalue.
I want to send the data to the template and then graph it using Highcharts.JS. From what I understand, I need to set up models.py to query the database, which is installed in Django, convert it to JSON, and then send this to the template for graphing.
Can anyone tell me if this is the right syntax to get the id, date, and value, and how to send the date and value in json format to the template?
This is the applicable model part:
class Absolutedollarvalue(models.Model):
cik = models.CharField(max_length=16)
date = models.DateField()
value = models.DecimalField(max_digits=65535, decimal_places=65535)
class Meta:
managed = False
db_table = 'absolutedollarvalue'
Below is my views.py code:
from django.shortcuts import render
from django.http import HttpResponse
from django.db import connection
from toa.models import Absolutedollarvalue as adv
import psycopg2
import json
import datetime
def adv(request):
adv.objects
a = adv(id = '1103804')
return render(request,toa/graphs.html,a)
This is my template:
<html>
<head>
<title> Fund Performance </title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>
</head>
<body>
<div id="chart_container" style="width:100%; height:400px;"></div>
<script type="text/javascript">
$(document).ready(function() {
var absolutedollarval = {
chart: {
renderTo: 'chart_panel',
type: 'time-series',
},
legend: {enabled: false},
title: {text: 'Daily NAV'},
xAxis: {title:{text:'Date'}},
yAxis: {title: {text: 'dollar value'}}
};
var chartDataUrl = "{% url 'chart_data_json' %}?name = avg_by_day"
+"&days=14";
$.getJSON(chartDataUrl,
function(data){
absolutedollarval.xAxis.categories=
absolutedollarval.series[0].name =
absolutedollarval.series[0].data =
var chart = new Highcharts.Chart(absolutedollarval);
});
});
</script>
</body>
</html>
Really appreciate any help you can give. Thanks!
Edit:
Is this how you would push the value to the template if I want the date and value column?
def adv(request):
dates = adv.objects.filter(cik = '1103804').values('date')
vals = adv.objects.filter(cik ='1103804').value('absolutedollarvalue')
data = serializers.serialize('json', dates)
data2 = serializers.serialize('json',vals)
return render(request,'toa/graphs.html',{"dates":data,"values":data2})
Upvotes: 1
Views: 2639
Reputation: 4933
Send value in JSON format to template:
from django.core import serializers
def adv(request):
adv.objects
a = adv(id = '1103804') #Will fetch all the column
a = adv(id = '1103804').values('columnName') #Will fetch the particular column.
data = serializers.serialize('json', a)
context = Context(data)
return render(request,'toa/graphs.html',context)
render(request, "foo.html", {'jsonKey': jsonValue}) #For passing individual JSON
Upvotes: 2