Django formsets:从模型formset中的列表初始化额外字段的值

时间:2014-06-17 04:52:48

标签: django forms django-templates django-forms

我正在尝试生成一个模仿视图的表单,以便更轻松地输入数据。这是一个记分表,其中玩家是顶部列标题,每个玩家有两个子标题。除了必须使用预制django插入播放器的方式之外,表单当前生成并正确提交。 This是表单模板的当前html输出,显示了几个记录和额外的表单(------按钮对于所有行都是相同的,如在查询集填充的行中所反映的,这就是我想要在额外内容中或在保存时初始化的内容。

  

表格

class ScoreForm(ModelForm):
    game = IntegerField(widget=HiddenInput())

    class Meta:
        model = Score
        exclude = ['chouette']
        widgets={'score': TextInput(attrs={'size': 6}), 'position': TextInput(attrs={'size':3})}
  

查看

def manage_scores(request, chnum=1):    
    chobj = Chouette.objects.get(number = chnum)
    chid = chobj.id
    p_list = Score.objects.filter(chouette = chid).distinct('player').order_by('player').values_list('player', 'player__handle')
    playernum = p_list.count()
    lastgamequery = Score.objects.filter(chouette = chid).latest('game')
    newgame = lastgamequery.game + 1
    ScoreFormSet = modelformset_factory(Score, form = ScoreForm, extra = playernum)
    if request.method == "POST":
        formset = ScoreFormSet(request.POST)
        for f in formset:
            f.fields['game'].initial=newgame
        if formset.is_valid():
            newscores = formset.save(commit = False)
            for newscore in newscores:
                newscore.chouette = chobj
                newscore.save()
            return redirect('update', chnum=chnum)
    else:
        formset = ScoreFormSet(queryset = Score.objects.filter(chouette = chid).order_by('game', 'player'))
        for f in formset:
            f.fields['game'].initial = newgame
    return render(request, "manage_scores.html", {'player_list' : p_list, 'formset' : formset, 'newgame' : newgame})
  

模板:

    <form method='post' action=''>
    {% csrf_token %}
    {{ formset.management_form }}
    <table class='table'>
        <thead>
            <tr><th></th>
            {% for player in player_list %}
            <th class="text-center" colspan="2" >{{ player.1 }}</th>
            {% endfor %}
            </tr>
            <tr>
            <th class="text-center">Game</th>
            {% for player in player_list %}
            <th class="text-center">Score</th><th class="text-center">Position</th>
            {% endfor %}
        <tbody>
        {% regroup formset by instance.game as game_list %}
        {% for game in game_list %}
            <tr><td class="text=center">
            {% if game.grouper %}
                {{game.grouper}}
            {% else %}
                {{ newgame }}
            {% endif %}
            </td>
            {% for g in game.list %}
                {{ g.id }}
                {{ g.game }}
                <td class="text-center">{{ g.player }}{{ g.score}}</td>
                <td class="text-center">{{ g.position }}</td>
            {% endfor %}   
        {% endfor %}</tr>
    </table>
    <input type="submit" class="btn btn-default btn-sm" role="button" value="Update" />
</form>

模板中当前的{{g.player}}允许我测试表单,但显然不是这里的答案。重新组合可以将所有内容放在正确的位置,并将查询集数据传递给表单集,但不能帮助我预先填充额外的表单。任何指导都非常受欢迎,并且对其余代码的无情攻击也是受欢迎的,因为我对此很新,并且几乎将它与重复的SO搜索拼凑在一起。

1 个答案:

答案 0 :(得分:0)

现在,我已经通过从表单中排除播放器字段并将播放器值传递到&#34; newscores&#34;来解决了这个问题。对象的传递方式与chouette值相同。来自视图的相关代码:

        if formset.is_valid():
            newscores = formset.save(commit = False)
            pid=0
            for newscore in newscores:
                newscore.chouette = chobj
                if newscore.game == newgame:
                    newscore.player = p_obj[pid]
                pid +=1
                newscore.save()