gpt4 book ai didi

python - 我需要有关如何在 Django 中实现复选框的建议

转载 作者:行者123 更新时间:2023-11-30 22:19:48 25 4
gpt4 key购买 nike

我正在尝试创建一种学生可以注册教师类(class)的方式。我向 StudentData 模型添加了一个 bool 字段,然后向我的 Course 模型添加了一个多对多字段。我的每个类(class)页面都是生成的 slug 页面,其中列出了所有学生。我希望每个学生附近都会显示一个复选框。如果老师选择更多学生并按下注册按钮,则只有这些学生可以查看类(class)。

现在,模板条件我可以自己完成,但我一直坚持使用复选框以正确的方式更新数据。这是样板:

<ul>
{% for student in students %}
<br>
<br>
{{ student.name }} {{ student.surname }}<input type='checkbox' {% ifequal value 0 %}checked{% endifequal %}> 0
<br>
<br>
{% endfor %}
</ul>
<div class="row">
<div class="col-md-4">
<div class="pagination">
<span class="step-links">
{% if students.has_previous %}
<a href="?page=1">&laquo; first</a>
<a href="?page={{ students.previous_page_number }}">previous</a>
{% endif %}
<span class="current">
{{ students.number }}
</span>
{% if students.has_next %}
<a href="?page={{ students.next_page_number }}">next</a>
<a href="?page={{ students.paginator.num_pages }}">last &raquo;</a>
{% endif %}
</span>
</div>
</div>
<div class="col-md-4">
<button class="btn-success">Enroll</button>
</div>
</div>


class StudentDataForm(forms.ModelForm):
enrolled = forms.BooleanField()

def __init__(self):
if self.checked:
self.fields['enrolled'].initial = True

class Meta:
model = StudentData
fields = ('enrolled', )


class StudentData(models.Model):
name = models.CharField(max_length=30)
surname = models.CharField(max_length=50)
student_ID = models.CharField(unique=True, max_length=14)
notes = models.CharField(max_length=255, default=None, blank=True)
enrolled = models.BooleanField(default=False)
course = models.ManyToManyField('Course', default=None, blank=True)

class Course(models.Model):
study_programme = models.ForeignKey('StudyProgramme', on_delete=models.CASCADE, default='')
name = models.CharField(max_length=50, unique=True)
ects = models.PositiveSmallIntegerField(validators=[MaxValueValidator(99)])
description = models.TextField()
year = models.PositiveSmallIntegerField(validators=[MaxValueValidator(99)])
semester = models.IntegerField(choices=((1, "1"),
(2, "2"),
), default=None)
teacher1 = models.ForeignKey('TeacherData', on_delete=models.CASCADE, default=None,
verbose_name="Course Teacher", related_name='%(class)s_course_teacher')
teacher2 = models.ForeignKey('TeacherData', on_delete=models.CASCADE, default=None, null=True,
verbose_name="Seminar Teacher", related_name='%(class)s_seminar_teacher')
slug = models.SlugField(max_length=150, unique=True)

最佳答案

为什么有 2 个类似的问题? Need advice on implementing feature regarding Django models

就个人而言,最好在Course模型中拥有与类(class)和学生相关的ManyToManyField。这很简单,因为在此之后,您所需要做的就是加载表单并发布它。然后:

class Course(models.Model):
# all your fields
students = models.ManyToManyField(StudentData)

class CourseForm(forms.ModelForm):
class Meta:
model = Course
fields = "__all__"

默认情况下,表单中的ManyToManyFieldSelectMultiple 小部件表示。如果您喜欢复选框:

class CourseForm(forms.ModelForm):
class Meta:
model = Course
fields = "__all__"
widgets = {
'students': forms.CheckboxSelectMultiple
}

在您的模板中,只需加载表单{{form}},教师就会通过复选框选择类(class)中的学生。每个学生都将通过使用反向 ManyToManyField 关系进行过滤来查看类(class):

student = Student.objects.get(name='John', surname='Doe')
courses = student.course_set.all() # These are all the courses where the student is enrolled

这就是ManyToManyField的优点。您可以从 CourseForm 分配学生,并使用反向关系 course_set 从学生模型访问他们。

关于python - 我需要有关如何在 Django 中实现复选框的建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49003113/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com