如何在Django中编辑/更新表单集?

时间:2020-01-03 05:58:16

标签: django django-models django-forms

我正在使用modelformset_factory创建一个表单集,并且成功创建了一个表单集。现在我想编辑表单集数据,该数据基本上包含3个字段,即学位,年份和大学,我将如何实现。这是我的代码:

views.py

    def edit_member(request,mem_id):
        mem_edit = MemberShip.objects.get(id=mem_id)
        context = {}
        quaFormset = modelformset_factory(Qualification, form=QualificationForm)    
        formset = quaFormset(request.POST or None, queryset=Qualification.objects.none(),prefix='qualification')     
        if request.method == 'POST':
            if formset.is_valid():
                formset.save()
    context = {'formset':formset}
    return render(request,'member/member_edit.html',context)

forms.py

    class QualificationForm(forms.ModelForm):
        class Meta:
            model = Qualification            
            fields = [
                'degree',
                'year',
                'college',
            ]      
            widgets = {
                'degree': forms.TextInput(attrs={'class': 'formset-field'}),
                'year': forms.TextInput(attrs={'class': 'formset-field'}),
                'college': forms.TextInput(attrs={'class': 'formset-field'})
            }

member_edit.html

<table class="table form-table table-bordered table-sm">
                    <thead class="text-center" style="background-color:red;">
                    <tr>
                        <th>Degree</th>
                        <th>Year</th>
                        <th>College</th>
                        <th></th>
                    </tr>
                    </thead>
                    <tbody>
                    {% for form_data in formset %}
                    <tr class="item">
                        <td><span style="color: red;"></span>
                            {{ form_data.degree }}
                        </td>
                        <td><span style="color: red;"></span>
                            {{ form_data.year }}
                        </td>
                        <td><span style="color: red;"></span>
                            {{ form_data.college }}
                        </td>
                        <td>
                            <button type="button" class="btn btn-danger btn-sm remove-form-row"
                                    id="{{ formset.prefix }}">

                                Delete
                            </button>
                        </td>
                    </tr>
                    {% endfor %}
                    <tr>
                        <td colspan="9"
                            style="border-left: none!important; border-right: none !important; border-bottom: none!important;">
                            <button type="button" class="btn btn-sm btn-success add-form-row"
                                    id="{{ formset.prefix }}">
                                Add
                            </button>
                        </td>
                    </tr>
                    </tbody>
                </table>
                {{ formset.management_form }}

0 个答案:

没有答案