Marcixen
Marcixen

Reputation: 31

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'

Upvotes: 2

Views: 1501

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

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

Related Questions