首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >编辑Django中的多个字段

编辑Django中的多个字段
EN

Stack Overflow用户
提问于 2022-02-15 17:43:10
回答 2查看 37关注 0票数 0

我在编辑代码中的页面时遇到了问题。基本上,我有一个页面,我有多个选择字段,我可以选择学生。但我有一个问题,在理解如何删除一个特定的学生,如果我需要编辑这个网页。让我更清楚地说明一些代码。

models.py

代码语言:javascript
复制
class TheorySyllabus(models.Model):
    name = models.CharField(max_length=100, null=True, blank=True)
    subject_duration = models.ManyToManyField(
        SubjectDuration, related_name='theory_syllabus')
    course_type = models.ForeignKey(
        CourseType, on_delete=models.DO_NOTHING, null=True, blank=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = 'Theory Syllabus'


class TheoryCourse(models.Model):
    name = models.CharField(max_length=100)
    student = models.ManyToManyField(Student, related_name='theory_courses')
    theory_syllabus = models.ForeignKey(
        TheorySyllabus, on_delete=models.DO_NOTHING, null=True, blank=True)
    is_active = models.BooleanField(default=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.name

views.py

代码语言:javascript
复制
def edit_theory_course(request, pk):
    theory_course = TheoryCourse.objects.get(id=pk)
    student_obj = theory_course.student.all()
    theory_syllabus = TheorySyllabus.objects.all()
    students = Student.objects.filter(is_active=True).order_by('last_name')
    context = {
        'theory_course': theory_course,
        'theory_syllabus': theory_syllabus,
        'students': students,
        'student_obj': student_obj,
    }
    if request.method == 'POST':
        course_name = request.POST.get('course_name')
        student = request.POST.getlist('student')
        syllabus = request.POST.get('syllabus')
        try:
            theory_course.name = course_name
            theory_course.theory_syllabus_id = syllabus
            theory_course.save()
            for stud in student:
                theory_course.student.add(stud)

            theory_course.save()

            messages.success(request, "Theoretical Course Edited")
            return HttpResponseRedirect(reverse('theory:course_list'))
        except:
            messages.error(request, "Failed to Edit Theoretical Course")
            return HttpResponseRedirect(reverse('theory:edit_theory_course', kwargs={'pk': pk}))
    return render(request, 'theory/edit_theory_course.html', context)

我知道,基本上,我需要做的是放置一个if语句来比较这两个列表,并删除(如果需要的话)不再是条目列表一部分的值。问题是,我不知道如何将这一逻辑。任何帮助都是非常感谢的。非常感谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-02-15 18:03:52

我认为您可以立即通过执行theory_course.student.set([list_of_student_pks])来替换完整的学生列表。

票数 1
EN

Stack Overflow用户

发布于 2022-02-15 18:21:01

实现这一点的一个非常简单的方法是对所有当前的学生进行查询(您甚至不需要这样做,因为它已经在theory_course中了),然后只需得到查询中的学生和提交的学生之间的区别。这将产生被删除的学生,然后可以对其进行迭代和删除。

代码语言:javascript
复制
current_students = theory_course.students
selected_students = Student.objects.filter(pk__in=student)
removed_students = current_students.difference(selected_students)

for s in removed_students:
    s.delete()

不过,可以使用和Object的set()方法一次性设置所有is,但从我的经验来看,删除用户列表以便进行必要的更改(例如发送电子邮件)要好得多。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71131018

复制
相关文章

相似问题

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