(隐藏字段 ID)选择一个有效的选项。该选择不是可用的选择之一。 (姜戈)

发布于 2025-01-12 04:16:08 字数 1925 浏览 0 评论 0原文

当我尝试提交两个表单集时收到此错误。填写表单并单击“保存”按钮后,出现错误:

(隐藏字段 ID)选择有效选项。该选择不是可用的选择之一。

我正在尝试创建动态表单,以便用户在单击“添加”按钮时可以添加新的部分以及该部分内的新讲座。添加新表单功能效果很好,只是将其保存到数据库时遇到问题。

Views.py

def addMaterials(request, pk):

course = Course.objects.get(id=pk)
sections = CourseSection.objects.filter(course_id=pk)
materials = CourseMaterial.objects.filter(section__in=sections)

SectionFormSet  = modelformset_factory(CourseSection, form=SectionForm, extra=0)
sectionformset = SectionFormSet(request.POST or None, queryset=sections)

MaterialFormSet  = modelformset_factory(CourseMaterial, form=MaterialForm, extra=0)
materialformset = MaterialFormSet(request.POST or None, queryset=materials)

context = { 
  'course': course, 
  'sectionformset': sectionformset,
  'materialformset': materialformset,
  }

if request.method == "POST":
  if all([sectionformset.is_valid() and materialformset.is_valid()]):
    for sectionform in sectionformset:
      section = sectionform.save(commit=False)
      section.course_id = course.id
      section.save()
      for materialform in materialformset:
        material = materialform.save(commit=False)
        print(material)
        material.section_id = section #section.id or section.pk also doesn't work
        material.save()
    return('success')

return render(request, 'courses/add_materials.html', context)

Forms.py

class SectionForm(forms.ModelForm):
  class Meta:
    model = CourseSection
    fields = ['section_name', ]
    exclude = ('course_id', )

class MaterialForm(forms.ModelForm):
  class Meta:
    model = CourseMaterial
    fields = ['lecture_name', 'contents']

第二个表单集(materialformset)需要第一个表单集中的部分 id,因此视图中有两个循环。 有人可以帮我解决这个问题吗?我不知道如何解决它。

这就是我正在尝试做的事情。 输入图片此处描述

I'm receiving this error when I try to submit two formsets. After I fill the form and click the save button, it gives the error:

(Hidden field id) Select a valid choice. That choice is not one of the available choices.

I'm trying to create dynamic form so that the user can add new sections and also new lectures inside the section when they click "Add" button. The adding new form function works well, I just have problem saving it to the database.

Views.py

def addMaterials(request, pk):

course = Course.objects.get(id=pk)
sections = CourseSection.objects.filter(course_id=pk)
materials = CourseMaterial.objects.filter(section__in=sections)

SectionFormSet  = modelformset_factory(CourseSection, form=SectionForm, extra=0)
sectionformset = SectionFormSet(request.POST or None, queryset=sections)

MaterialFormSet  = modelformset_factory(CourseMaterial, form=MaterialForm, extra=0)
materialformset = MaterialFormSet(request.POST or None, queryset=materials)

context = { 
  'course': course, 
  'sectionformset': sectionformset,
  'materialformset': materialformset,
  }

if request.method == "POST":
  if all([sectionformset.is_valid() and materialformset.is_valid()]):
    for sectionform in sectionformset:
      section = sectionform.save(commit=False)
      section.course_id = course.id
      section.save()
      for materialform in materialformset:
        material = materialform.save(commit=False)
        print(material)
        material.section_id = section #section.id or section.pk also doesn't work
        material.save()
    return('success')

return render(request, 'courses/add_materials.html', context)

Forms.py

class SectionForm(forms.ModelForm):
  class Meta:
    model = CourseSection
    fields = ['section_name', ]
    exclude = ('course_id', )

class MaterialForm(forms.ModelForm):
  class Meta:
    model = CourseMaterial
    fields = ['lecture_name', 'contents']

The second formset which is materialformset need the section id from the first formset hence why there is two loop in views.
Can someone help me to solve this. I'm not sure how to fix it.

This is the what I'm trying to do.
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赠我空喜 2025-01-19 04:16:08

我是 django 的新手,但我不得不面对同样的问题。我的解决方案是单独处理“views.py”中的每个表单集。

在 template.html 中,为您拥有的每个表单集创建一个标签,然后在该标签内放置 (请注意,名称很重要,必须与您提交的表格不同)。

然后在views.py中,改为编写 if all([sectionformset.is_valid() and Materialformset.is_valid()]),尝试如下:

if 'form1' in request.POST:
     if sectionformset.is_valid():
         sectionformset.save()
         # other rows of your code
     return('success')

if 'form2' in request.POST:
     if materialformset.is_valid():
         materialformset.save()
         # etc. etc.

I'm new to django but I had to face with the same problem. My solution was to handle singularly each formset inside 'views.py'.

In the template.html, create a tag for each formset you have, than inside that tag put <input type="submit" name="form1">(Note that name is important and must be different with the respect of the form you are submitting).

Then in views.py, instead for writing if all([sectionformset.is_valid() and materialformset.is_valid()]), try like this:

if 'form1' in request.POST:
     if sectionformset.is_valid():
         sectionformset.save()
         # other rows of your code
     return('success')

if 'form2' in request.POST:
     if materialformset.is_valid():
         materialformset.save()
         # etc. etc.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文