Possible to have pull downs from multiple tables at top of change list?

Is it possible to do this at the top of the change_list page for a specific model and then push selected papers using an admin action?

I cannot seem to get the various papers to display from the for loops!

{% block content %}
<div>
    <form method="post" action="{% url 'book_candidate' %}">
        {% csrf_token %}
        <table>
            <thead>
                <tr>
                    <th>Listening Paper</th>
                    <th>Reading Paper</th>
                    <th>Speaking Paper</th>
                    <th>Writing Paper</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>
                    </td>
                    <td>
                        <label for="listening_paper"></label>
                        <select name="listening_paper">
                            {% for listening in listenings %}
                            <option value="{{ listening.id }}">{{ listening.paper_name }}</option>
                            {% endfor %}
                        </select>
                    </td>
                    <td>
                        <label for="reading_paper"></label>
                        <select name="reading_paper">
                            {% for reading in readings %}
                            <option value="{{ reading.id }}">{{ reading.paper_name }}</option>
                            {% endfor %}
                        </select>
                    </td>
                    <td>
                        <label for="speaking_paper"></label>
                        <select name="speaking_paper">
                            {% for speaking in speakings %}
                            <option value="{{ speaking.id }}">{{ speaking.paper_name }}</option>
                            {% endfor %}
                        </select>
                    </td>
                    <td>
                        <label for="writing_paper"></label>
                        <select name="writing_paper">
                            {% for writing in writings %}
                            <option value="{{ writing.id }}">{{ writing.paper_name }}</option>
                            {% endfor %}
                        </select>
                    </td>
                    <td></td>
                </tr>
            </tbody>
        </table>
        <button type="submit" class="button" title="Book Selected Candidates">Book Selected Candidates</button>
    </form>
</div>
{{ block.super }}
{% endblock %}
{% endblock %}```

![image|493x101](upload://dulPGyIHWrLwKPLYMNTSxqzrjgW.png)

Read the first two paragraphs at The Django admin site | Django documentation | Django.

This is one of those situations where it really seems to me that you’re going to be a whole lot better off doing this in your own view than trying to wedge this into the admin.

1 Like

Thank you, Ken! I have tried to create views that loop over the contents of each of the tables for Listening, Reading, Speaking and Writing papers and have managed to pull that data into the pull downs. Then iterate over a candidates queryset to generate a table for each candidate but when clicking Book Selected Candidates it only pulls the last candidate across from the table. I am assuming that this is because that is all that is stored in the candidate placeholder by this point. I thought that maybe I could store in array during each iteration but got a bit stuck.


{% block object-tools-items %}
{% block content %}
<div>
    <form method="post" action="{% url 'book_candidate' %}">
        {% csrf_token %}
        <table>
            <thead>
                <tr>
                    <th>SWELT Date</th>
                    <th>Listening Paper</th>
                    <th>Reading Paper</th>
                    <th>Speaking Paper</th>
                    <th>Writing Paper</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>
                        <label for="listening_paper"></label>
                        <select name="listening_paper">
                            {% for listening in listenings %}
                            <option value="{{ listening.id }}">{{ listening.paper_name }}</option>
                            {% endfor %}
                        </select>
                    </td>
                    <td>
                        <label for="reading_paper"></label>
                        <select name="reading_paper">
                            {% for reading in readings %}
                            <option value="{{ reading.id }}">{{ reading.paper_name }}</option>
                            {% endfor %}
                        </select>
                    </td>
                    <td>
                        <label for="speaking_paper"></label>
                        <select name="speaking_paper">
                            {% for speaking in speakings %}
                            <option value="{{ speaking.id }}">{{ speaking.paper_name }}</option>
                            {% endfor %}
                        </select>
                    </td>
                    <td>
                        <label for="writing_paper"></label>
                        <select name="writing_paper">
                            {% for writing in writings %}
                            <option value="{{ writing.id }}">{{ writing.paper_name }}</option>
                            {% endfor %}
                        </select>
                    </td>
                    <td></td>
                </tr>
            </tbody>
        </table>
        <button type="submit" class="button" title="Book Selected Candidates">Book Selected Candidates</button>
    </form>
</div>
{{ block.super }}
{% endblock %}
{% endblock %}```
    candidates = Candidate.objects.all()
    examdates = ExamDate.objects.all()
    listenings = ExamPaper.objects.filter(paper_type=1)
    readings = ExamPaper.objects.filter(paper_type=2)
    speakings = ExamPaper.objects.filter(paper_type=3)
    writings = ExamPaper.objects.filter(paper_type=4)

    if request.method == 'POST':
        selected_candidates = request.POST.getlist('selected_candidates[]')
        candidate_id = request.POST.get('candidate[]')
        exam_date_id = request.POST.get('exam_date')
        listening_paper_booked_id = request.POST.get('listening_paper')
        reading_paper_booked_id = request.POST.get('reading_paper')
        speaking_paper_booked_id = request.POST.get('speaking_paper')
        writing_paper_booked_id = request.POST.get('writing_paper')

        for candidate in selected_candidates:
            Listening.objects.create(candidate_id=candidate_id, paper_id=listening_paper_booked_id)
            Reading.objects.create(candidate_id=candidate_id, paper_id=reading_paper_booked_id)
            Speaking.objects.create(candidate_id=candidate_id, paper_id=speaking_paper_booked_id)
            Writing.objects.create(candidate_id=candidate_id, paper_id=writing_paper_booked_id)

            print(f"Booking Candidate {candidate_id} with papers:")
            print(f"Listening Paper: {listening_paper_booked_id}")
            print(f"Reading Paper: {reading_paper_booked_id}")
            print(f"Speaking Paper: {speaking_paper_booked_id}")
            print(f"Writing Paper: {writing_paper_booked_id}")

    messages.success(request, "Candidates successfully booked.")

    return render(request, 'book_candidate.html', {'candidates': candidates, 'listenings': listenings, 'readings': readings, 'speakings': speakings, 'writings': writings})