[Answered ]-Django for-loop only displaying the last iteration

1👍

You are overwriting the new variable with a new QuerySet of size 1 in your for loop. Try replacing the following:

recommend = recommendations(book_id, 3) 
for i in recommend: 
    new = Book.objects.filter(id=i) #to get the Book id from the model 
    print(new) #just to check if it's working

with this:

recommend = recommendations(book_id, 3) 
new = Book.objects.filter(id__in=recommend)
print(new)

This will fetch all Book instances with an id value that exists in the recommend list.

👤Selcuk

Leave a comment