首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >选择有效的选项Django Filtered下拉菜单

选择有效的选项Django Filtered下拉菜单
EN

Stack Overflow用户
提问于 2021-01-20 00:28:08
回答 1查看 54关注 0票数 0

开发人员,

在我的项目中,我有一个表单,其中有一个具有学生姓名选择的字段,它是当前在该特定班级注册的学生的下拉字段。它从表“部分注册”中获取此信息,然后检查主“学生”表。过滤工作正常,但是当我提交表单时,它显示学生姓名不是有效的选择。我猜是因为它提交的是学生姓名,而不是ID,我不是100%确定。这是我的模型和视图。我不知道该怎么解决这个问题。感谢你的帮助。

代码语言:javascript
复制
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() 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-20 01:32:12

看起来问题出在这里:

代码语言:javascript
复制
 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。因此,我认为表单字段将由不正确的数据作为字段。如果我是对的,解决方案可能是:

代码语言:javascript
复制
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')

或者:

代码语言:javascript
复制
student_selection = Student.objects.filter(sectionenrollment_set__section_id=classid).order_by('student_name')
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65795568

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档