How can I ask questions randomly to different user in django?

147 Views Asked by At

I want to create a list of questions and from it I want to ask random 5 questions to each logged in user.How can i do it. where can I create a list of quetions and options.How can I render questions and answer to html page ?

I tried to create a db table and used it to get values in html template But it did not work.

class Quetions(models.Model):
    quetion =models.CharField(max_length=100,default='')
    option1=models.CharField(max_length=20,default='')
    option2=models.CharField(max_length=20,default='')
    option3=models.CharField(max_length=20,default='')
    option4 =models.CharField(max_length=20, default='')
1

There are 1 best solutions below

1
webbyfox On

While fetching the question, you can do something like

pks = Quetions.objects.values_list('pk', flat=True)
random_idx = random.sample(range(0, len(pks)-1), 5)
random_questions = Quetions.objects.filter(pk__in=random_idx)