The following works you must select 10 questions:
def pregunta(request):
#este selecciona un pack de 10 números de pregunta
respuestas = Respuesta.objects.filter(idUsuario=281,modalidadPreguntas='operarioDeServicios',respuestasCorrectas__lt=4)[:10]
#este selecciona todas (all) pero debería filtrar sólo los 10 números que ha recopilado respuestas:
preguntas = Pregunta.objects.filter(modalidadPregunta='operarioDeServicios')
How do I make questions filter by numbers that have collected answers?
Example:
answers = [1,3,4,5,7,8,9,11,14,16]
That questions do not have all(), but 10 questions whose question number is those indicated in the answers tuple or array.
I do not know if I explained well.
Responses table class:
(This is where all the user results are collected. This is where you have to take the 10 question numbers < 4 (Correct Answers) and then select questions from the table based on those 10 numbers.
class Respuesta(models.Model):
idUsuario = models.CharField(max_length=200,null=True, blank=True)
modalidadPreguntas = models.CharField(max_length=200,null=True, blank=True)
numeroPregunta = models.IntegerField(null=True,blank=True)
respuestasCorrectas = models.IntegerField(null=True,blank=True)
Questions table class:
class Pregunta(models.Model):
preguntaTest = models.TextField(null=True, blank=True)
modalidadPregunta = models.CharField(max_length=200, null=True, blank=True)
numeroPregunta = models.IntegerField(null=True, blank=True)
Query in table Answers of 10 question numbers whose Correct answer is less than 4. And with that tuple of 10 question numbers, in the Questions table take tuple of 10 questions according to those 10 numbers:
ids_respuestas = []
for respuesta in respuestas.values():
ids_respuestas.append(respuesta.get("numeroPregunta"))
preguntas = Pregunta.objects.filter(modalidadPregunta='operarioDeServicios', numeroPregunta__in=ids_respuestas)
return render(request, "pregunta/pregunta.html",{'preguntas':preguntas,'respuestas':respuestas})
All the best.
You can use the
__in
to search for multiple elements.If I'm not mistaken this should work for you.
I hope it is useful to you.
In the end it worked like this: answer takes the query as a tuple, which we then pass to questions . We return questions and display it on the page using for.