I'm having a problem while continuing to develop a project in django which consists of doing test questions ...
I have seen the need to use 2 tables of my models.py a table Question and other table Options, the problem is that I can not get the result of both, and also shows me an error
I show you my code to see if you can lend me a hand to understand what is wrong and how I can solve it ... thanks!
Models.py
class Pregunta(models.Model):
id = models.AutoField(primary_key=True)
textopregunta = models.CharField('Texto pregunta', max_length=1000)
test = models.ForeignKey(Test, on_delete=models.CASCADE)
def __str__(self):
return self.textopregunta
class Opcion(models.Model):
id = models.AutoField(primary_key=True)
correcta = models.BooleanField('Opcion Correcta')
opcion = models.CharField('texto de la opcion', max_length=500)
puntuacion = models.IntegerField(default=100)
pregunta = models.ForeignKey(Pregunta, on_delete=models.CASCADE)
def __str__(self):
return self.opcion
urls.py
url(r'^test/(?P<pk>[0-9]+)/$', views.TestDetail.as_view(), name='run'),
views.py
class TestDetail(generic.DetailView):
model = Pregunta
context_object_name = 'pregunta'
template_name = 'html/prueba.html'
def get_queryset(self): # Devuelve un iterable con la lista de elementos que forman parte del controlador
return Pregunta.objects.filter('textopregunta')
html prueba.html
{% block content %}
{% for test in pregunta %}
<p>Pregunta: {{ pregunta.textopregunta }}</p><br/>
<p>Opciones: {{ opcion.opcion }}</p>
{% endfor %}
{% endblock %}
I have been consulting the documentation and I have found this fragment, which I think is where the mixing of tables takes place ... but I do not understand it.
<ul>
{% for choice in question.choice_set.all %}
<li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}</li>
{% endfor %}
</ul>
I also have doubts if I should use a detailview or listwiew, because I want to see the content of a particular test obtained through its id in the previous template ...
I hope you can teach me my mistakes, thanks!