AvK wrote:
> hi,
> i want to get random records form the database, the queryset looks
> like this :
> "
> questions = Question.objects.filter(quiz__pk=quiz_id).order_by('?')
> [:10]
>
> "
> the problem is that if i want to use questions variable again, it
> everytimes changes the returned records from db for a new random and
> that is not desired. i mean if i have code like this :
> ""
> def create_quiz_forms(quiz_id, data=None):
>
> questions =
> Question.objects.filter(quiz__pk=quiz_id).order_by('?')[:10]
> print questions
> #and then i have this:
> form_list = []
> for pos, question in enumerate(questions):
> form_list.append(QuestionForm(question, data,
> prefix=pos))
> if not form_list:
> # No questions found, so the quiz_id must have been bad.
> raise Http404('Invalid quiz id.')
> return form_list
>
> ""
> the ferst reference to the questions variable (print questions) will
> give another effects(records) than in the second reference in the for
> loop.(for pos, question in enumerate(questions).
> how can i make the quesions variable in a differents references
> returned the same records from the db.
>
Try replacing
questions =
Question.objects.filter(quiz__pk=quiz_id).order_by('?')[:10]
with
questions =
list(Question.objects.filter(quiz__pk=quiz_id).order_by('?')[:10])
That way the question list is fixed. It would also make more sense to test
immediately whether the question list is empty rather than waiting until you
build the forms, by following that with
if not questions:
raise Http404('Invalid quiz id.')
regards
Steve
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---