如何使用将显示formset.has_changed()的初始数据创建formset为False?

时间:2016-09-26 08:26:30

标签: django django-forms

Django 1.10。

学习表单集。即 has_changed()方法。 文档:https://docs.djangoproject.com/en/1.10/topics/forms/formsets/#django.forms.formsets.BaseFormSet.total_error_count

如果获取请求,我的示例始终显示formset已更改。稍后我们会在邮寄请求中检查它。但到目前为止只是新创建的formset。

好吧,我无法创建一个新的表单集,表明它没有更改。 换句话说,新创建的表单集一次显示它已更改。

请看一下生成的html。我从那里复制了初始数据的密钥。

文档中的示例不是示范性的。但我自己的例子没有按预期工作。

问题在于formset中的form [0]。换句话说,表格[0] .has_changed = True。

顺便说一下!这将显示formset.has_changed = False:

initial_data = [{'form-0-title': '', 'form-0-pub_date': ''}]

但真正的初始数据呢?在表格的情况下,有一些初始数据:https://docs.djangoproject.com/es/1.10/ref/forms/api/#checking-which-form-data-has-changed

不,空表格不是我们的方法。

你能帮助我吗?

views.py

class General(View):
    initial_data = [{'title': 'Initial title 0', 'pub_date': '2015-01-01'},     
                    {'title': 'Initial title 1', 'pub_date': '2016-01-01'}]
    ArticleFormSet = formset_factory(ArticleForm, extra=2, max_num=3) 

    def get_formset(self):

        data = {
                'form-TOTAL_FORMS': '3',
                'form-INITIAL_FORMS': '2',
                "form-MIN_NUM_FORMS": '0',
                "form-MAX_NUM_FORMS": '3',
                'form-0-title': self.initial_data[0].get('title'),                
                'form-1-title': self.initial_data[1].get('title'),
                'form-0-pub_date': self.initial_data[0].get('pub_date'),                
                'form-1-pub_date': self.initial_data[1].get('pub_date'),
                'form-2-title': "",
                'form-2-pub_date': "" 
                }

        formset = self.ArticleFormSet(
                                    data=data, 
                                    initial=self.initial_data
        )
        self.has_changed = formset.has_changed() # For debugging.

        return formset

    def get_context_data(self):        
        context = {'formset': self.get_formset()}
        return context

    def get(self, request):
        template_name = "general/article.html"
        context = self.get_context_data()
        return render(request, template_name, context)        

forms.py

from django import forms

class ArticleForm(forms.Form):
    title = forms.CharField()
    pub_date = forms.DateField()

article.html

<form id="form" method="post">
    {% csrf_token %}
    {{ formset.management_form }}
    {% for form in formset %}
    <h2>{{ forloop.counter0 }}</h2>
            <table>
                {{ form.as_table }}
            </table>
    {% endfor %}
    <input id="submit" type="submit" value="Submit" />
</form>

<h2>Formset changed?</h2>
<p>{{ formset.has_changed }}</p>

<h2>form[0] changed?</h2>
<p>{{ formset.0.has_changed }}</p>

<h2>form[1] changed?</h2>
<p>{{ formset.1.has_changed }}</p>

HTML

<form id="form" method="post">
    <input type='hidden' name='csrfmiddlewaretoken' value='vWQq0Yl5gbVe7x1iKFX7uLCUlAL3RpBnQAQGIsRt7pwuJeqU7VDl3OE544Wt2oSN' />
    <input id="id_form-TOTAL_FORMS" name="form-TOTAL_FORMS" type="hidden" value="3" /><input id="id_form-INITIAL_FORMS" name="form-INITIAL_FORMS" type="hidden" value="2" /><input id="id_form-MIN_NUM_FORMS" name="form-MIN_NUM_FORMS" type="hidden" value="0" /><input id="id_form-MAX_NUM_FORMS" name="form-MAX_NUM_FORMS" type="hidden" value="3" />

    <h2>0</h2>
            <table>
                <tr><th><label for="id_form-0-title">Title:</label></th><td><input id="id_form-0-title" name="form-0-title" type="text" value="Initial title 0" /></td></tr>
<tr><th><label for="id_form-0-pub_date">Pub date:</label></th><td><input id="id_form-0-pub_date" name="form-0-pub_date" type="text" value="2015-01-01" /></td></tr>
            </table>

    <h2>1</h2>
            <table>
                <tr><th><label for="id_form-1-title">Title:</label></th><td><input id="id_form-1-title" name="form-1-title" type="text" value="Initial title 1" /></td></tr>
<tr><th><label for="id_form-1-pub_date">Pub date:</label></th><td><input id="id_form-1-pub_date" name="form-1-pub_date" type="text" value="2016-01-01" /></td></tr>
            </table>

    <h2>2</h2>
            <table>
                <tr><th><label for="id_form-2-title">Title:</label></th><td><input id="id_form-2-title" name="form-2-title" type="text" /></td></tr>
<tr><th><label for="id_form-2-pub_date">Pub date:</label></th><td><input id="id_form-2-pub_date" name="form-2-pub_date" type="text" /></td></tr>
            </table>

    <input id="submit" type="submit" value="Submit" />
</form>

<h2>Formset changed?</h2>
<p>True</p>

<h2>form[0] changed?</h2>
<p>True</p>


<h2>form[1] changed?</h2>
<p>True</p>

0 个答案:

没有答案