简体   繁体   English

Select 有效选择 Django 过滤下拉菜单

[英]Select a valid choice Django Filtered Drop Down Menu

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.在我的项目中,我有一个表单,其中包含一个学生姓名选择字段,它是当前就读于该特定 class 的学生的下拉字段。 It gets this information from table Section Enrollment than checks the master Student table.它从表 Section Enrollment 中获取此信息,而不是检查 master Student 表。 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.我的猜测是因为它提交的是学生姓名而不是 ID,我不能 100% 确定。 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() 


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 . values_list('studentpsid_id__student_name', flat = True)正在收集学生name而不是他们的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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM