扩展基于类的UpdateView以对对象进行评级

时间:2013-04-03 18:33:47

标签: django django-forms

我需要以下模型来创建一个表单,该表单允许更新现有的响应(先前使用slug生成然后通过电子邮件发送给响应者)并为每个{创建RatingCV中{1}}。在Django中最简单的方法是什么?目前我有一个基于类的UpdateView for Response,就是这样。

CV.objects.all()

1 个答案:

答案 0 :(得分:0)

我最终弄清楚如何做到这一点。如果有人有兴趣,我的代码如下。

def add_response(request):
    CVs = CV.objects.all()
    if request.method == "POST":
        ResForm = ResponseForm(request.POST, instance=Response())
        RatForms = [RatingForm(request.POST, prefix=str(cv.id), instance=Rating(cid=cv)) for cv in CVs]
        if ResForm.is_valid() and all([rf.is_valid() for rf in RatForms]):
            new_response = ResForm.save(commit=False)
            new_response.submit_time = datetime.now()
            new_response.submited = True
            new_response.save()
            for rf in RatForms:
                new_rating = rf.save(commit=False)
                new_rating.rid = new_response
                new_rating.save()
            return HttpResponseRedirect('/thanks/')
        else:
            for i, _ in enumerate(RatForms):
                RatForms[i].cv = CV.objects.filter(id=int(RatForms[i].prefix))[0]
                print RatForms[i].cv
            return render(request, 'response.html', {'response_form': ResForm, 'rating_forms': RatForms})
    else:
        ResForm = ResponseForm(instance=Response())
        RatForms = [RatingForm(prefix=str(cv.id), instance=Rating(cid=cv)) for cv in CVs]

        for i, _ in enumerate(RatForms):
            RatForms[i].cv = CV.objects.filter(id=int(RatForms[i].prefix))[0]
            print RatForms[i].cv

        return render(request, 'response.html', {'response_form': ResForm, 'rating_forms': RatForms})