R
R
Ramon Yaskal2017-06-16 18:00:34
Django
Ramon Yaskal, 2017-06-16 18:00:34

Django output slice using order_by('?')[:n]?

Instead of 5 I want to put the variable n

answers = Answer.objects.all().order_by('?')
answers = answers[:5]
n I take from the database. How to do what you want? Answer base, take everything, mix, take the first n and send to html

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Ramon Yaskal, 2017-06-17
@ramonyaskal


n = guestionNum.objects.all()
n=n[0].question_num

G
Gasoid, 2017-06-16
@Gasoid

def example_view(request, n):
    answers = Answer.objects.all().order_by('?')[:n]
    context = {'answers': answers}
    return render(request, 'template.html', context)

Need to consider order_by('?') very slow query

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question