django modelformset_factory正在为我的模型添加一个虚假的“id”字段,我希望它能够停止

时间:2012-02-07 17:30:41

标签: django django-forms

如标题所示,使用modelformset_factory生成一组模型,它似乎为每个表单添加了一个额外的选择字段,标记为'id'。我想阻止它。

[编辑:我在下面发布了一个解决方案,但这肯定是一个黑客攻击。如果有一种“适当”的方式来抑制这种情况,我非常想知道它。如果没有,我不介意找出是否有充分理由不这样做。]

以下是formset的代码:

from models import ArticlesTemplate
class ExistingTemplateForm(ModelForm):
    selected = forms.BooleanField(required=False, initial=False, widget=forms.RadioSelect(), label = 'use?')
    class Meta:
        model =  ArticlesTemplate
        exclude = ('template_file', 'organisation_for', 'mime_type', 'id', 'pk',)


ExistingTemplateFormset = modelformset_factory(ArticlesTemplate, extra = 0, form=ExistingTemplateForm)

(请注意,将, exclude=('id',)添加到modelformset_factory调用不会阻止添加此字段。

这是modelform自己生成的html:

In [15]: print i_h.ExistingTemplateForm()
<tr><th><span id="for-id_original_filename-">Original filename:</span></th><td><input id="id_original_filename" type="text" name="original_filename" maxlength="100" /></td></tr>
<tr><th><span id="for-id_tags-">Tags:</span></th><td><input type="text" name="tags" id="id_tags" /><br /><span class="helptext">A comma-separated list of tags.<
/span></td></tr>
<tr id="selected_row" class="selected_row "><th><span id="for-id_selected_0-">use?</span></th><td><ul>

</ul></td></tr>

正如所料,没有id字段。

这是formset生成的html:

In [24]: print etf

<input type="hidden" name="form-TOTAL_FORMS" value="1" id="id_form-TOTAL_FORMS" /><input type="hidden" name="form-INITIAL_FORMS" value="1" id="id_form-INITIAL_F
ORMS" /><input type="hidden" name="form-MAX_NUM_FORMS" id="id_form-MAX_NUM_FORMS" />
<tr><th><span id="for-id_form-0-original_filename-">Original filename:</span></th><td><input id="id_form-0-original_filename" type="text" name="form-0-original_
filename" value="FakeExampleCompanyName.docx" maxlength="100" /></td></tr>
<tr><th><span id="for-id_form-0-tags-">Tags:</span></th><td><input type="text" name="form-0-tags" value="&quot;England and Wales&quot;, &quot;Model articles wit
h amendments&quot;, &quot;Private company limited by shares&quot;" id="id_form-0-tags" /><br /><span class="helptext">A comma-separated list of tags.</span></td
></tr>
<tr id="selected_row" class="selected_row "><th><span id="for-id_form-0-selected_0-">use?</span></th><td><ul>

</ul></td></tr>
<tr><th><span id="for-id_form-0-id-">Id:</span></th><td><select name="form-0-id" id="id_form-0-id">
<option value="" selected="selected">---------</option>
<option value="1">ArticlesTemplate object</option>
</select></td></tr>

正如你所看到的那样,最后有select元素,我没有要求。

3 个答案:

答案 0 :(得分:2)

请改用form.visible_fields。

例如:

            {% for form in formset %}
                {% for field in form.visible_fields %}
                    {{ field.label_tag }} {{ field }}
                {% endfor %}
            {% endfor %}

答案 1 :(得分:0)

这是我找到的解决方案,我承认它并不完全令人满意:

class ExistingTemplateFormset(modelformset_factory(ArticlesTemplate, extra = 0, form=ExistingTemplateForm)):
    def __init__(self, *args, **kwargs):
        super(ExistingTemplateFormset, self).__init__(*args, **kwargs)
        for x in self: x.fields['id'].widget = forms.HiddenInput()

令我惊讶的是,这是一个无法解决的解决方案:

class ExistingTemplateForm(ModelForm):
    selected = forms.BooleanField(required=False, initial=False, row_renderer = default_renderer, widget=forms.RadioSelect(), label = 'use?')
    id = forms.ModelChoiceField(widget = forms.HiddenInput(), queryset = ArticlesTemplate.objects.all())
    class Meta:
        model =  ArticlesTemplate
        exclude = ('template_file', 'organisation_for', 'mime_type',)
## This __init__ has no effect on the widget used to render id
    def __init__(self, *args, **kwargs):
        super(ExistingTemplateForm, self).__init__(*args, **kwargs)
        self.fields['id'] = forms.ModelChoiceField(widget = forms.HiddenInput(), queryset = ArticlesTemplate.objects.all())
        self.fields['id'].widget = forms.HiddenInput()

答案 2 :(得分:0)

我也无法解决这个问题。但我找到了一个解决方法:

我将没有标签的id字段渲染为带有标记{{form.id}}的隐藏字段。输出例如是:<input id="id_form-0-id" name="form-0-id" type="hidden" value="1" />

但是,如果使用{{ formset }}或{{1}}等标记渲染表单集              {form for formset%}         {{form}}         {%endfor%}     

您将成为id字段前的可见标签。

我希望我能提供帮助