Django内联Formset问题(列表超出范围)

时间:2011-02-18 03:07:11

标签: django inline-formset

这应该很简单,我实际上已经在昨天工作了。我不知道发生了什么变化,但它现在抛出一个错误。

def game_design(request):
    user=User.objects.get(pk=request.user.id)
    organization=user.organization_set.all()[0]
    website=organization.website_set.all()[0]
    surveys=website.survey_set.all()
    error=''
    SurveyFormSet=inlineformset_factory(Website, Survey, extra=0, can_delete=True)
    NavigationFormSet=modelformset_factory(Navigation, extra=1)
    if request.method=='POST':
        survey_formset=SurveyFormSet(request.POST, request.FILES, prefix="surveys")
        navigation_formset=NavigationFormSet(request.POST, request.FILES, prefix="navigations")
        if survey_formset.is_valid() and navigation_formset.is_valid():
                survey_formset.save()
            navigation_formset.save()
            return HttpResponseRedirect("/rewards/")
        else:   
            error="Please fix your errors"
           survey_formset=SurveyFormSet(request.POST, request.FILES,prefix="surveys")
           navigation_formset=NavigationFormSet(request.POST, request.FILES,prefix="navigations")
           return render_to_response('website/game_design.html', {'website':website,'survey_formset':survey_formset, 'navigation_formset':navigation_formset, 'error':error}, context_instance=RequestContext(request))
    else:
         survey_formset=SurveyFormSet(instance=website,prefix="surveys")
         navigation_formset=NavigationFormSet(queryset=Navigation.objects.none(),prefix="navigations")
    return render_to_response("website/game_design.html", {'website':website,'survey_formset':survey_formset,'navigation_formset':navigation_formset,'error':error},context_instance=RequestContext(request))

感谢您对此提供任何帮助

2 个答案:

答案 0 :(得分:9)

我的问题似乎与我的Formset中不包含实例参数有关。

我应该有以下内容:

survey_formset = SurveyFormSet(request.POST,request.FILES,instance = website,prefix =“surveys”)

慢慢地但我肯定会学会不犯愚蠢的错误

答案 1 :(得分:1)

您应该始终发布完整的追溯,以便人们可以提供更好的帮助。

如果你说没有任何改变并且它在昨天工作,那么有一个区域代码不需要改变以抛出IndexError

organization=user.organization_set.all()[0]  
website=organization.website_set.all()[0]

你确定不是问题吗?

无论哪种方式,直到我们看到追溯,很难说;)