Devs,
In my project I have a form that has a field that has a student name selection, it is a drop down field by the students that are currently enrolled in that particular class. It gets this information from table Section Enrollment than checks the master Student table. The filtering works out correctly, however when I submit my form, it says the student name is not a valid choice. My guess is because its submitting that student name and not a ID, I'm not 100% sure. Here is my models and view. I don't know how to fix this. Appreciate that help.
QUERY IN QUESTION:
getstudents = SectionEnrollment.objects.filter(section_id=classid).select_related().values_list('studentpsid_id__student_name', flat = True)
MODELS:
# Creation of Classrooms and Assigned Teachers
class SectionEnrollment(models.Model):
sectionenrollmentpsid = models.CharField(primary_key = True,max_length = 50, default = "")
section = models.ForeignKey(Section,on_delete = models.PROTECT, default = "" ,)
studentpsid = models.ForeignKey(Student,on_delete = models.PROTECT, default = "" ,)
entry_date = models.DateField(blank=True)
exit_date = models.DateField(blank=True)
dropped = models.BooleanField(default = False, blank = True)
class Meta:
verbose_name = "Student Section Enrollment"
def __str__(self):
return self.sectionenrollmentpsid
# Where Basic Student Data Is Stored
class Student(models.Model):
studentpsid= models.CharField(primary_key = True , default = "", max_length = 50, unique = True)
student_name = models.CharField(max_length = 50)
first_name = models.CharField(max_length = 50, default = "")
last_name = models.CharField(max_length = 50,default = "")
gender = models.CharField(max_length = 1,default = "")
student_grade = models.CharField(max_length = 2, default = "")
home_room = models.CharField(max_length = 5, default = "")
student_enrollment = models.CharField(max_length = 2, default = "")
school_number = models.CharField(max_length = 15, default = "")
email = models.EmailField(default = "")
projected_graduation_year = models.CharField(max_length = 4, default = "")
counseling_goal = models.TextField(max_length = 255)
win_username = models.CharField(max_length = 50)
win_password = models.CharField(max_length = 50)
offsite_laptop = models.BooleanField(default = False, blank = True)
image = models.ImageField(default ="default.png", upload_to ='student_pics')
VIEW:
@login_required
def Rapid_Fire(request, classid):
if request.method == "GET":
date = datetime.date.today()
class_name = Section.objects.filter(sectionpsid=classid)
getstudents = SectionEnrollment.objects.filter(section_id=classid).select_related().values_list('studentpsid_id__student_name', flat = True)
student_selection = getstudents.all().order_by('studentpsid__student_name')
my_class_id = request.session['my_class_id']
sectionpsid = Section.objects.get(sectionpsid = my_class_id)
form = Rapid_Fire_Form()
form.fields["student_name"].queryset = getstudents
form.fields["sectionpsid"].queryset = class_name
context = ({'form': form, 'my_class_id': my_class_id, 'sectionpsid':sectionpsid,})
return render(request, 'points/rapid_fire.html', context )
elif request.method == "POST":
date = datetime.date.today()
class_name = Section.objects.filter(sectionpsid=classid)
getstudents = SectionEnrollment.objects.filter(section_id=classid).select_related().values_list('studentpsid_id__student_name', flat = True)
student_selection = getstudents.all().order_by('studentpsid__student_name')
my_class_id = request.session['my_class_id']
sectionpsid = Section.objects.get(sectionpsid = my_class_id)
form = Rapid_Fire_Form(request.POST)
form.fields["student_name"].queryset = getstudents
form.fields["sectionpsid"].queryset = class_name
if form.is_valid():
# Records logged in user to table
obj = form.save(commit= False)
userid = request.user
obj.created_by = userid
obj.save()