I’m trying to do a form query on a field and the goal is to get all the kids enrolled in all the teachers classes in this drop down menu. The query I have right now is getting all the classes a teacher is enrolled in . However how do I retrieve the kids within those classes. I attached a picture to so you can visually see how I linked the tables.
JavaScript
x
84
84
1
FORMS.PY
2
3
class Select_Student_Phone_Log_Form(forms.ModelForm):
4
class Meta:
5
model = PhoneLog
6
fields = ('student_ps',)
7
labels = {'student_ps':_('Student Name') }
8
9
def __init__(self, *args, **kwargs):
10
self.user = kwargs.pop('user')
11
super (Select_Student_Phone_Log_Form,self).__init__(*args,**kwargs )
12
students = SectionEnrollment.objects.filter(section_id__teacher_username = self.user)
13
self.fields['student_ps'].queryset= students
14
15
MODELS.PY
16
17
# Section Information Stored
18
class Section(models.Model):
19
sectionpsid= models.CharField(primary_key = True, default = "", max_length = 50)
20
schoolpsid = models.ForeignKey(School,on_delete = models.CASCADE, default = "" ,)
21
coursepsid = models.ForeignKey(Course,on_delete = models.CASCADE, default = "" ,)
22
termpsid = models.ForeignKey(Term,on_delete = models.CASCADE, default = "" ,)
23
section_number = models.CharField(default = "", max_length = 50)
24
expression = models.CharField(default = "", max_length = 50)
25
external_expression= models.CharField(default = "", max_length = 50)
26
staffpsid = models.ForeignKey(Staff,on_delete = models.CASCADE, default = "" ,)
27
gradebooktype = models.CharField(default = "", max_length = 50)
28
teacher_username = models.ManyToManyField(User)
29
30
# Creation of Classrooms and Assigned Teachers
31
class SectionEnrollment(models.Model):
32
sectionenrollmentpsid = models.CharField(primary_key = True,max_length = 50, default = "")
33
section = models.ForeignKey(Section,on_delete = models.PROTECT, default = "" ,)
34
studentpsid = models.ForeignKey(Student,on_delete = models.CASCADE, default = "" ,)
35
entry_date = models.DateField(blank=True)
36
exit_date = models.DateField(blank=True)
37
dropped = models.BooleanField(default = False, blank = True)
38
39
40
41
# Where Basic Student Data Is Stored
42
class Student(models.Model):
43
studentpsid= models.CharField(primary_key = True , default = "", max_length = 50, unique = True)
44
student_name = models.CharField(max_length = 50)
45
first_name = models.CharField(max_length = 50, default = "")
46
last_name = models.CharField(max_length = 50,default = "")
47
gender = models.CharField(max_length = 1,default = "")
48
student_grade = models.CharField(max_length = 2, default = "")
49
home_room = models.CharField(max_length = 5, default = "")
50
student_enrollment = models.CharField(max_length = 2, default = "")
51
school_number = models.CharField(max_length = 15, default = "")
52
email = models.EmailField(default = "")
53
projected_graduation_year = models.CharField(max_length = 4, default = "")
54
counseling_goal = models.TextField(max_length = 255)
55
win_username = models.CharField(max_length = 50)
56
win_password = models.CharField(max_length = 50)
57
offsite_laptop = models.BooleanField(default = False, blank = True)
58
image = models.ImageField(default ="default.png", upload_to ='student_pics')
59
60
# Stores Phone Log Information
61
class PhoneLog(models.Model):
62
student_ps = models.ForeignKey(Student,on_delete = models.CASCADE, default ="")
63
created_at = models.DateTimeField(default = timezone.now)
64
comments = models.TextField(max_length = 355, default= "")
65
RESULT_CHOICES = [
66
(None, 'PLEASE SELECT A RESULT'), # THIS IS OPTIONAL
67
(1, 'Spoke to Mom'),
68
(2, 'Spoke to Dad'),
69
(3, 'Spoke to Guardian'),
70
(4, 'Left message'),
71
(5, 'Messages Full'),
72
(6, 'Sent Email'),
73
(7, 'Other'),
74
]
75
result = models.PositiveSmallIntegerField(choices = RESULT_CHOICES)
76
created_by = models.ForeignKey(User, on_delete = models.PROTECT)
77
78
class Meta:
79
verbose_name = "K8-Phone Log"
80
81
def __str__(self):
82
return self.student_ps.student_name
83
84
Advertisement
Answer
I would try this :
JavaScript
1
11
11
1
class Select_Student_Phone_Log_Form(forms.ModelForm):
2
class Meta:
3
model = PhoneLog
4
fields = ('student_ps',)
5
labels = {'student_ps':_('Student Name') }
6
7
def __init__(self, *args, **kwargs):
8
self.user = kwargs.pop('user')
9
super(Select_Student_Phone_Log_Form, self).__init__(*args, **kwargs )
10
self.fields['student_ps'].queryset = Student.objects.filter(sectionenrollment__section__teacher_username=self.user)
11