'AttributeError: 'QuerySet' object has no attribute 'objects', breaking the query attributes
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'
Solution 1:[1]
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")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Willem Van Onsem |
