Django: how to solve several Foreign key relationship problem?

I’m currently learning Django and making electronic grade book. I am completely stuck after trying everything, but still cannot solve the problem. I will explain in detail and post all the relevant code below.

I need to have two url pages “class_students” and “teacher_current”. The first page is for the teacher to see the list of students of a certain class. The table on this page has “action” column. In every cell of this column there is View button, so that the teacher could be redirected to “teacher_current” page and see the list of current tasks, given to a certain student. On this page there is a “Mark” column, its cells may contain EITHER mark given to this student with link to another page to update or delete this mark OR “Add mark” link to add mark on another page.

Here comes the problem: everything works correctly, except the thing that each mark is related to a Task class via Foreign key and NOT to a Student class. So every student of the same class has the same marks for the same tasks. But it certainly won’t do. Here is all my relevant code:

  1. views.py models.py urls.py: CodePile | Easily Share Piles of Code
  2. relevant templates: CodePile | Easily Share Piles of Code
    What I tried: Firstly I thought I could just check, if each mark related to the student, but not the task, so I changed my teacher_student_current.html like this:
<td>
        {% if task.mark_set.get and task.mark_set.get.student_id == student %}
        <a href="{% url 'mark_update' pk=task.mark_set.get.id %}">{{ task.mark_set.get }}</a>
        {% else %}
        <a href="{% url 'mark_add' pk=student.id rel_task=task.id %}">Add mark</a>
        {% endif %}
    </td>

Adding different marks for the same tasks to different students of the same class works, however their reflection in the table doesn’t work.

I also tried changing many things in the template but frequently got an error get() returned more than one Mark – it returned number! Certainly, as mark is related to a task and not a student any attempts to get mark from a student were a failure.

Afterwards, I tried filtering in views.py with filter and even Q loookups, for instance:

def teacher_student_current(request, pk):
    student = Student.objects.get(pk=pk)
    tasks_queryset = Task.objects.filter(mark__student_id=student).order_by('end_date')[:10]
    current_date = datetime.datetime.now()
    if "btnform1" in request.POST:
        return redirect("/school/student/all/")
    context = {
        "student": student,
        "current_date": current_date,
        "tasks_queryset": tasks_queryset,
    }
    return render(request, "teacher_student_current.html", context)

or

def teacher_student_current(request, pk):
    student = Student.objects.get(pk=pk)
    tasks_queryset = Task.objects.filter(
        Q(mark__student_id=None) |
        Q(mark__student_id=student)
    ).order_by('end_date')[:10]
    current_date = datetime.datetime.now()
    if "btnform1" in request.POST:
        return redirect("/school/student/all/")
    context = {
        "student": student,
        "current_date": current_date,
        "tasks_queryset": tasks_queryset,
    }
return render(request, "teacher_student_current.html", context)

But nothing helps, because, even if filtration works creating given mark to a certain task is already assigned only to one student of this class. And there is not even such task for another student. I am sure I miss the logic and don’t know how to tackle at all. I will be glad for your help, even if you just advise me what to google.

I’m having a hard time figuring out what you’re looking for from your description.

The query you’re trying to create appears to be for a Task object. What task object are you trying to retrieve?

This does not appear to be true based on the models posted. The code shown has Mark with two foreign keys, one each to Student and Task.


Thanks a lot for your answer. What I’m looking for is any way to make students of the same class not to have the same marks for the same tasks, as they have in the photos

Ok, I’m trying to take a step back here. Let’s completely ignore the UI for the moment and focus on your query. (The UI should only be rendering data retrieved by the view - get the view right, and the rendered page will be correct.)

well, I see. I actually tried every query. For instance, I tried different Q lookups with & and |, retrieving tasks, related to a particular student and tasks with not added marks yet. Different combinations wouldn’t work either

“Regardless, in principle, you will need to get the student id, find the marks that belong to that student id for the particular class id, and then get the tasks related to those marks. You may not be able to get them in one step” - the advice of a guy from another forum. I will try it