同一网页上表单和表单集的关系

发布于 2024-12-07 10:14:28 字数 1697 浏览 2 评论 0原文

我试图将表单集中创建的所有对象与表单集在同一网页上创建的对象相关联。因此,代码示例如下:

def create_b(request):
    SpeciesFormSet = modelformset_factory(Species, fields=('common', 'scientific'))
    if request.method == 'POST':
        formset = SpeciesFormSet(request.POST)
        form1 = BForm(request.POST)
        if form1.is_valid():
            objcreate = BModel.objects.create(
                                name = form1.cleaned_data['name'],
                                ...
                                )
            objcreate.save()
            for forms in formset.forms:
                if forms.is_valid():
                    formset1 = Species.objects.create (
                                common = forms.cleaned_data['common'],
                                scientific = forms.cleaned_data['scientific'],
                                BName = form1.cleaned_data['name']
                                )
                    formset1.save()
                else:
                    formset = SpeciesFormSet()
                    form1 = BForm()
                    c = {'SpeciesFormSet' : SpeciesFormSet, 'form1' : form1}
                    c.update(csrf(request))
                    return render_to_response('Forms/create_b.html', c)
            return HttpResponseRedirect('/accounts/profile')
    else:
        formset = SpeciesFormSet()
        form1 = BForm()
    c = {'SpeciesFormSet' : SpeciesFormSet, 'form1' : form1}
    c.update(csrf(request))
    return render_to_response('Forms/create_b.html', c)

我遇到的问题是,当尝试将表单集对象与表单对象关联时,它告诉我表单对象实际上并不存在。然而,它在数据库中创建对象,但不创建任何表单集对象。我收到错误“无法分配“u''”:“Species.BName”必须是“BModel”实例。”如果有帮助的话。此外,该关系是一个外键。有办法解决这个问题吗?感谢您抽出时间。

I'm trying to relate all the objects created in a formset to an object created on the same webpage as the formset. So, an example of the code is this:

def create_b(request):
    SpeciesFormSet = modelformset_factory(Species, fields=('common', 'scientific'))
    if request.method == 'POST':
        formset = SpeciesFormSet(request.POST)
        form1 = BForm(request.POST)
        if form1.is_valid():
            objcreate = BModel.objects.create(
                                name = form1.cleaned_data['name'],
                                ...
                                )
            objcreate.save()
            for forms in formset.forms:
                if forms.is_valid():
                    formset1 = Species.objects.create (
                                common = forms.cleaned_data['common'],
                                scientific = forms.cleaned_data['scientific'],
                                BName = form1.cleaned_data['name']
                                )
                    formset1.save()
                else:
                    formset = SpeciesFormSet()
                    form1 = BForm()
                    c = {'SpeciesFormSet' : SpeciesFormSet, 'form1' : form1}
                    c.update(csrf(request))
                    return render_to_response('Forms/create_b.html', c)
            return HttpResponseRedirect('/accounts/profile')
    else:
        formset = SpeciesFormSet()
        form1 = BForm()
    c = {'SpeciesFormSet' : SpeciesFormSet, 'form1' : form1}
    c.update(csrf(request))
    return render_to_response('Forms/create_b.html', c)

The problem I'm having is that when trying to relate the formset object to the form one, it tells me that the form object doesn't actually exist. It creates the object in the database however, just none of the formset objects. I get the error "Cannot assign "u''": "Species.BName" must be a "BModel" instance." if that helps. Also, the relationship is a ForeignKey. Is there anyway to solve this? Thanks for your time.

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

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

发布评论

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

评论(2

夏九 2024-12-14 10:14:28

这个确切的模式正是 内联模型表单集 的用途。

This exact pattern is what inline model formsets are for.

不顾 2024-12-14 10:14:28

从错误来看,您似乎正在分配一个 BName,其中属性应该是 BModel。您想要使用 BModel 更改 BName,或者需要修复您的模型,以便它们与物种的 BName 匹配。

formset1 = Species.objects.create (
    common = forms.cleaned_data['common'],
    scientific = forms.cleaned_data['scientific'],
    BName = form1.cleaned_data['name']
)
formset1.save()

From the error, it looks like you are assigning a BName where the property is supposed to be a BModel. Either, you want to change the BName with a BModel, or you need to fix your models so they match with a BName for the Species.

formset1 = Species.objects.create (
    common = forms.cleaned_data['common'],
    scientific = forms.cleaned_data['scientific'],
    BName = form1.cleaned_data['name']
)
formset1.save()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文