Random different values on each request

23 views
Skip to first unread message

cs.s...@gmail.com

unread,
Nov 17, 2022, 9:21:51 AM11/17/22
to Django users
Hello friends,
I am trying to get 2 random values on each response using order_by('?')[:2], but getting the same response each time.  also tried RandomManager() as described here.  https://stackoverflow.com/questions/962619/how-to-pull-a-random-record-using-djangos-orm , but still getting the same response.   (SQL query:  SELECT TOP 2 * FROM QuizBank ORDER BY NEWID())

views.py:
class GetEssayQuestion(APIView):
    def get(self, request):
        data = Quizbank.objects.all().filter(status=1, section=5).order_by('?')[:2]
        serializer = QuizbankSerializer(data, many=True)
        return Response(serializer.data)

Appreciate any reference? Thanks.

RANGA BHARATH JINKA

unread,
Nov 19, 2022, 4:19:40 AM11/19/22
to django...@googlegroups.com
Hi,

You can use random module in python to generate random numbers 

Thanks and Regards

J. Ranga Bharath
Cell: 9110334114
   

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/800a6bc3-9870-47dd-8f41-54f13de108bdn%40googlegroups.com.

subin

unread,
Nov 19, 2022, 12:30:53 PM11/19/22
to django...@googlegroups.com
Please let me know if that sounds okay.

subin

unread,
Nov 19, 2022, 12:31:25 PM11/19/22
to django...@googlegroups.com
Please reach out to Sophie at .

On Sat, Nov 19, 2022 at 10:19 AM RANGA BHARATH JINKA <bharath...@gmail.com> wrote:

subin

unread,
Nov 19, 2022, 12:32:24 PM11/19/22
to django...@googlegroups.com
Thanks for reaching out! This inbox is not monitored. Please call this number or email us at

On Sat, Nov 19, 2022 at 10:19 AM RANGA BHARATH JINKA <bharath...@gmail.com> wrote:

subin

unread,
Nov 19, 2022, 12:33:02 PM11/19/22
to django...@googlegroups.com
Please let me know if that is acceptable with you.

On Sat, Nov 19, 2022 at 10:19 AM RANGA BHARATH JINKA <bharath...@gmail.com> wrote:

subin

unread,
Nov 19, 2022, 4:05:30 PM11/19/22
to django...@googlegroups.com
Thanks again for all your help.

--
Reply all
Reply to author
Forward
0 new messages