Django Inlineformset_工厂。我向表单中添加了多个数据。仅保存最新的数据

2024-09-30 08:15:23 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的问题中,我使用inlineformset_工厂在1个表单上使用2个关系模型

在我的ciranta模型中

Ciranta类(模型.模型)

cek=models.ForeignKey(cekler,on_delete=models.CASCADE)

错误是,当我运行此代码时,我添加了多个ciranta模型,但最近的一个正在保存。我怎样才能解决这个问题

我的form.py

class CekForm(ModelForm):
    class Meta:
        model =cekler
        exclude = ()

class CirantaForm(ModelForm):
    class Meta:
        model = ciranta
        exclude = ()

CirantaFormSet = inlineformset_factory(cekler, ciranta,
                                       form=CirantaForm, extra=1)

我的观点.py

class CekCreate(CreateView):
    model = cekler
    fields = ['borclu', 'borcluAdres', 'borclu_mail', 'borcluTelefon','tutar','kur','banka','cek_tarih']


class CirantaCreate(CreateView):
    model = cekler
    fields = ['borclu', 'borcluAdres', 'borclu_mail', 'borcluTelefon','tutar','kur','banka','cek_tarih']
    success_url = reverse_lazy('home')

    def get_context_data(self, **kwargs):
        data = super(CirantaCreate, self).get_context_data(**kwargs)
        if self.request.POST:
            data['cirantalar'] = CirantaFormSet(self.request.POST)
        else:
            data['cirantalar'] = CirantaFormSet()
        return data

    def form_valid(self, form):
        context = self.get_context_data()
        cirantalar = context['cirantalar']
        with transaction.atomic():
            self.object = form.save()

            if cirantalar.is_valid():
                cirantalar.instance = self.object
                cirantalar.save()
        return super(CirantaCreate, self).form_valid(form)

和我的模板

  <table class="table">
            <br>
                {{ cirantalar.management_form }}

                {% for form in cirantalar.forms %}
                    {% if forloop.first %}
                        <thead class="thead-light">
                        <tr>
                            {% for field in form.visible_fields %}
                                <th>{{ field.label|capfirst }}</th>
                            {% endfor %}
                        </tr>
                        </thead>
                    {% endif %}
                    <tr class="{% cycle row1 row2 %} formset_row">
                        {% for field in form.visible_fields %}
                            <td>
                                {# Include the hidden fields in the form #}
                                {% if forloop.first %}
                                    {% for hidden in form.hidden_fields %}
                                        {{ hidden }}
                                    {% endfor %}
                                {% endif %}
                                {{ field.errors.as_ul }}
                                {{ field }}
                            </td>
                        {% endfor %}
                    </tr>
                {% endfor %}
            </table>
            <input type="submit" class ="btn btn-outline-primary btn-lg btn-block" value="Kaydet"/><br> <br>


Tags: in模型selfformfieldfieldsdatamodel

热门问题