D
D
Denis Pupchenko2017-03-28 19:50:10
Django
Denis Pupchenko, 2017-03-28 19:50:10

How to get the specified number of records from the database?

Model:

class Section(MPTTModel):
    class Meta:
        db_table='Section'
    section_text = models.CharField(verbose_name='Название раздела', max_length=100)
    parent = TreeForeignKey(verbose_name='Родительский Раздел',to='self', null=True, blank=True, related_name='children')

class Question(models.Model):
    class Meta:
        db_table = 'Question' 
    section = models.ForeignKey(Section)
    question_text = models.CharField(verbose_name='Описание вопроса', max_length=250)

Sample:
<form action="/vibor_test/gener/" method ="post">
     {% csrf_token %}
    <ol>
        {% recursetree nodes.get_descendants %}
            <li>
                {{ node.section_text }}
                {% if node.level == 1 %}
                    <input type="number" name="text" id="{{ node.id }}" value="0" >
                {% endif %}

                {% if node.is_leaf_node %}

                    <input type="checkbox" name="checkbox" value="{{ node.id }}" >
                {% endif %}
                {% if not node.is_leaf_node %}
                    <ol class="children">
                        {{ children}}
                    </ol>
                {% endif %}

            </li>
        {% endrecursetree %}
    </ol>
     <input class="button"  type="submit" value="Пройти тест">
 </form>

There is a template in which you need to specify the required number of questions.
It looks like this
04e7f0fc2b534a27b60661f386ca3695.png
. I can get all records from the database of the selected checkbox, using the feedback.
views.py
def gener(request):
    if request.method == 'POST':
        textbox_list = request.POST.getlist('text')
        chekbox_list = request.POST.getlist('checkbox')
        for x in chekbox_list:
            y = Section.objects.get(pk=x).question_set.all()
    return render(request, 'gener.html',{'question':y})

How to determine that the entered number belongs to a particular group of questions with checkboxes in order to display the required number?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Egor Kazantsev, 2017-03-29
@saintbyte

This is a tree, if you need a scroll page, then unload the top-level nodes in the right amount, then build a tree from them.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question