Reputation: 31
I have error after making "double query" request in my views.py how could I easily get around this problem ?
if request.method == 'POST':
index= request.POST.get('dropdown_index')
stocks= Indexes.objects.filter(Symbol=index)
Open = stocks.objects.values("Open")
High = stocks.objects.values("High")
Close = stocks.objects.values("Close")
Low = stocks.objects.values("Low")
mysite\main\views.py", line 18, in HomeView
Open = stocks.objects.values("Open")
AttributeError: 'QuerySet' object has no attribute 'objects'
Upvotes: 2
Views: 1501
Reputation: 477641
Your stocks
is already a QuerySet
, so you can obtain the values with:
if request.method == 'POST':
index = request.POST.get('dropdown_index')
stocks = Indexes.objects.filter(Symbol=index)
Open = stocks.values("Open")
High = stocks.values("High")
Close = stocks.values("Close")
Low = stocks.values("Low")
This will however make four queries to the database, you can fetch the values for Open
, High
, Close
and Low
all in the same query from the database:
if request.method == 'POST':
index = request.POST.get('dropdown_index')
stocks = Indexes.objects.filter(Symbol=index)
data = stocks.values("Open", "High", "Close", "Low")
Upvotes: 1