Saturday, November 19, 2022

Re: Random different values on each request

Hi,

You can use random module in python to generate random numbers 

Thanks and Regards

J. Ranga Bharath
Cell: 9110334114
   

On Thu, 17 Nov 2022, 7:51 pm cs.s...@gmail.com, <cs.sabya@gmail.com> wrote:
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.

--
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+unsubscribe@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.

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAK5m317RvY8e3zW-e1hHS5r66grz5KvztWuuwJ0KzvPTODAUKA%40mail.gmail.com.

No comments:

Post a Comment