[Answered ]-Single object from queryset

1👍

The way you have it set up now, a StudentTrainingCourse can have only ONE student but, because a ForeignKey is a ManyToOne relationship, MANY students can have that same StudentTrainingCourse. What I think you want is for a Student to have ONE StudentTrainingCourse, but a StudentTrainingCourse to have MANY Students. My guess is what you need is:

class Student(models.Model):
    ...

    training_course = models.ForeignKey(StudentTrainingCourse, on_delete=models.CASCADE)
    
    ...

Then you can get the particular mission id like:

def student_mission_list(request, pk):
    student = Student.objects.get(id=pk)
    training_course = student.training_course
    mission = Mission.objects.get(training_course=training_course)
    # If you just want the mission ID:
    mission_id = mission.pk

    missions = StudentMission.objects.filter(
        student_training_course_id=student_training_course)

    context = {
        'student': student,
        'missions': missions,
    }
    return render(request, 'mission/student_mission_list.html', context)
    

Leave a comment