Django 表单:另一个 inlineformset_factory 中的 inlineformset_factory

问题描述

我目前的任务是按音调标记评论

TONAL_TYPES = ['positive','negative','neutral','undefined']

为此,我正在为一组“专家”构建一项服务,该服务将考虑每条评论 3 次,并根据“多数规则”做出决定。

我有以下型号:

class Result(models.Model):
    text = models.TextField(null=True)
    title = models.TextField(null=True)

class Comment(models.Model):
    text = models.TextField(blank=True,null=True)
    result = models.ForeignKey(Result,models.DO_nothing,null=True)

class CommentRound(models.Model):
    comment = models.ForeignKey(Comment,blank=True,null=True)
    tonal_type = models.CharField(choices=TONAL_TYPES,default='1')

在第一轮专家需要创建结果添加评论评论做第一个标记

目前前两件事正在使用以下代码(简化):

[views.py]

from django.forms import inlineformset_factory
from ..forms import ResultForm,CommentForm
from ..models import Result,Comment

def createResult(request):
    CommentFormSet = inlineformset_factory(Result,Comment,form=CommentForm,can_delete=False,extra=3,max_num=1)
    if request.method == 'POST':
        form = ResultForm(request.POST)
        if form.is_valid():
            result = form.save()
            formset = CommentFormSet(request.POST,instance=result)
            if formset.is_valid():
                formset.save()
                return redirect('/')
            else:
                logger.debug(formset.errors)
        else:
            logger.debug(form.errors)
    else:
        form = ResultForm()
        formset = CommentFormSet()
    render(request,'my_app/result_form.html',{'form': form,'formset': formset})

[forms.py]

from django.forms import ModelForm
from django import forms
class CommentForm(ModelForm):
    class Meta:
        model = Comment

class ResultForm(ModelForm):
    class Meta:
        model = Result

如何向表单集中的每条评论添加一个或多个“CommentRound”实例? (在这个例子中,'CommentRound' 被大大简化了;实际上,其中有相当多的评估参数。)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)