空formset传递验证但不保存记录

时间:2014-12-30 02:30:45

标签: django django-forms django-views

我的设置涉及一个表单,其中创建了游戏实例和团队绑定游戏。与游戏相关的一组球队输入名为GameTeamsFormset的表格集。

我的问题是,有时候GameTeamsFormset中的字段是空的,在我的数据库中创建空记录(是的,我想要这个)但是即使formset通过验证也没有实际创建记录我可以通过管理员手动添加这些空记录。 (好吧,他们并不是完全空的,因为他们有一个外键将他们绑定到Game实例)。那么,如何让我的视图为我的formset中的每个空表单保存记录?

Views.py

def addGame(request, league_id):
    if request.method == 'POST':

        league = League.objects.get(pk = league_id) # get the league object
        gameform = GameForm(request.POST) # initialize gameform with the POST data
        if gameform.is_valid():
            newgame = gameform.save(commit=False)
            newgame.league = league # set the league

            gameteamsform = GameTeamsFormset(request.POST, instance=newgame)
            if gameteamsform.is_valid():
                newgame.save() # save the game
                gameteamsform.save()
                return editleague(request, league_id)

            else:
                return HttpResponseRedirect("/") #needs fixin
        else:
            return HttpResponseRedirect("/") #needs fixin
    else:
        return HttpResponseRedirect("/") #needs fixin

Forms.py

class GameForm(ModelForm):
    class Meta:
        model = Game
        fields = ['game_heading', 'game_date', 'game_time']
        widgets = {
            'game_heading':TextInput(attrs={'type':'text', 'class':'form-control input-md'}),
            'game_date':TextInput(attrs={'type':'date', 'class':'form-control'}),
            'game_time':TextInput(attrs={'type':'time', 'class':'form-control'}),
        }


GameTeamsFormset = inlineformset_factory(Game, GameTeams, extra=2,
    fields=['team', 'placed', 'score'],
    widgets={'team': Select(attrs={'class':'form-control'}),
             'placed': Select(attrs={'class':'form-control'}),
             'score': TextInput(attrs={'class':'form-control'})},
    )

1 个答案:

答案 0 :(得分:2)

FormSets旨在忽略空记录。您必须单独保存(并可能设置相关密钥)。

所以在你的邮政编码中:

if request.POST:
    forms_factory = modelformset_factory(...)
    forms = forms_factory(request.POST)
    if forms.is_valid():
        for form in forms:
            form.instance.game = newgame
            form.save()

我相信这也将遍历返回的空白表单。

def addGame(request, league_id):
    if request.method == 'POST':

        league = League.objects.get(pk = league_id) # get the league object
        gameform = GameForm(request.POST) # initialize gameform with the POST data
        if gameform.is_valid():
            newgame = gameform.save(commit=False)
            newgame.league = league # set the league

            gameteamsform = GameTeamsFormset(request.POST, instance=newgame)
            if gameteamsform.is_valid():
                newgame.save() # save the game
                for gameteam in gameteamsform:
                    gameteam.game = newgame
                    gameteam.save()
                gameteamsform.save()
                return editleague(request, league_id)

            else:
                return HttpResponseRedirect("/") #needs fixin
        else:
            return HttpResponseRedirect("/") #needs fixin
    else:
        return HttpResponseRedirect("/") #needs fixin