簡體   English   中英

Select 有效選擇 Django 過濾下拉菜單

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

開發人員,

在我的項目中,我有一個表單,其中包含一個學生姓名選擇字段,它是當前就讀於該特定 class 的學生的下拉字段。 它從表 Section Enrollment 中獲取此信息,而不是檢查 master Student 表。 過濾工作正常,但是當我提交表單時,它說學生姓名不是一個有效的選擇。 我的猜測是因為它提交的是學生姓名而不是 ID,我不能 100% 確定。 這是我的模型和視圖。 我不知道如何解決這個問題。 感謝您的幫助。


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


看來問題出在這里:

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

values_list('studentpsid_id__student_name', flat = True)正在收集學生name而不是他們的id 所以我認為表單字段將是錯誤數據的字段。 如果我是對的,解決方案可能是:

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

或者:

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