0

So I'm currently working in Django and I wanted to grab data from my database and use it in my js script. This is what I'm trying to pass into js, stockhist=StockHistory.objects.filter(user_id=x).values()

2 Answers2

0

You can convert the queryset to JSON as described in the first answer to this question and return the results in your API response.

Depending on the nature of your project, you may also want to learn more about Django Rest Framework's serializers.

Mike
  • 785
  • 1
  • 6
  • 14
0

So I figured it out but had a hard time getting a dictionary instead of the sting I was getting when I used

```stockhist=(StockHistory.objects.filter(user_id=x))
hist=serialize('json',stockhist)```

A lucky guess led me to the right answer when I change the json to python like this

```stockhist=(StockHistory.objects.filter(user_id=x))
hist=serialize('python',stockhist)```

For those wondering how I called it in my html this is what I did

```{{ hist|json_script:"Hist" }}``` 

and I called it in my js like this

var performanceHist = JSON.parse(document.getElementById('Hist').textContent)