我在用着 exclude
在我的表单的Meta类中,从我的表单中排除我想要以编程方式填写的字段,但它仍然在表单中显示。
以下是代码的一些摘录:
# Model
class Info(models.Model):
completed_by = models.ForeignKey(User, related_name='+')
# Form
class InfoForm(forms.ModelForm):
class Meta:
model = Info
exclude = ('created_by',) #ETA: added comma to make this a tuple
widgets = {
'some_other_field': forms.HiddenInput(),
'some_other_field2': forms.DateInput(attrs={'readonly': True}),
}
# View
form = InfoForm(initial={'some_other_field': value},
prefix='info', instance=info)
return direct_to_template(request, 'myapp/info.html', locals())
# Template
<form class='uniForm' method='POST'>
{% csrf_token %}
<fieldset class='inlineLabels'>{{ form|as_uni_form }}</fieldset>
<input type='submit' name='action' value='Save' />
</form>
这看起来应该很简单,我知道我之前已经成功完成了。我删除/重新创建了我的数据库并清除了我的浏览器缓存,只是为了确保这不是一个因素。我也尝试过制作它 HiddenInput
田野,就像 some_other_field
(这是一个 ForeignKey
字段也),但它仍然出现在表格上。
这里有什么东西我不见了吗? uni_form会以某种方式覆盖设置吗?如果没有,有什么建议我可以在调试中寻找什么,看看这是怎么回事?
(使用Django 1.2.7版)