0

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() 


Ray Zuchowski
  • 363
  • 2
  • 12
  • confirm that you're using django forms e.g `forms.ModelForm` or an HTML form? – mh-firouzjah Jan 19 '21 at 16:35
  • Django.ModelForm . If I take off the student_name part of the query and just have the list of student ids in the list instead, it tells me that the following section must be a instance of the Student table when i hit submit. – Ray Zuchowski Jan 19 '21 at 16:39
  • OK if it is a model form, so way you're trying to give its field some value? it would have the values required by default? it's done by Django – mh-firouzjah Jan 19 '21 at 16:47
  • I'm querying all the student names down to the students in a particular class. If I didn't filter it, then all the student names would appear throughout the system. I don't want that. – Ray Zuchowski Jan 19 '21 at 16:49
  • yes, I got that. but what about the template? are you iterating the fields? specially the dropdown? or what? could add the templates? – mh-firouzjah Jan 19 '21 at 16:53
  • There is no iterating in the field, the query that you have for get students goes into the Section Enrollment table, filters the student_ids down to the class_id . Then i jump to the student table and pull the names from the student table. Thats all im doing. So no for loops or anything. – Ray Zuchowski Jan 19 '21 at 16:56
  • by `iteration` I mean are you doing something like this: https://stackoverflow.com/questions/9221010/how-do-i-iterate-over-the-options-of-a-selectfield-in-a-template – mh-firouzjah Jan 19 '21 at 17:07
  • No i am not doing anything like that. – Ray Zuchowski Jan 19 '21 at 17:25

1 Answers1

1

it seems the problem is here:

 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')

the values_list('studentpsid_id__student_name', flat = True) is collecting the students name not their id. so the form field will be field by incorrect data I think. and if I'm right the solution may be:

students_id = SectionEnrollment.objects.filter(section_id=classid).select_related().values_list('studentpsid__id', flat = True)
student_selection = Student.objects.filter(id__in=students_id).order_by('student_name')

or:

student_selection = Student.objects.filter(sectionenrollment_set__section_id=classid).order_by('student_name')
mh-firouzjah
  • 834
  • 1
  • 6
  • 15
  • It wants the student table field, an instance of it. I just did a test and wrote the query as getstudentsall = Students.objects.all() works fine . However that doesnt limit the students down to the class. I have to rewrite my original query but i have to start in the students table. – Ray Zuchowski Jan 19 '21 at 18:08