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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了空formset传递验证但不保存记录相关的知识,希望对你有一定的参考价值。

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

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

views.朋友

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.朋友

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'})},
    )
答案

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

以上是关于空formset传递验证但不保存记录的主要内容,如果未能解决你的问题,请参考以下文章

django formset 未验证,因为需要 ID

访问django formset数据

如果空值传递给Django中的表单字段,如何验证表单?

Django Formset管理-表单验证错误

Laravel 更新密码通过验证但不更新记录

Laravel 5 - 根据需要验证数组,但允许传递一个空数组