2

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'

1 Answer 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")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.