为什么我的排除字段仍然以这种Django形式出现? [英] Why does my excluded field still appear in this Django form?
问题描述
我在表单的Meta类中使用 exclude
从表单中排除要以编程方式填写的字段,但该字段仍显示在表单中.
I'm using exclude
in my form's Meta class to exclude a field from my form that I want to fill in programatically, but it's still showing up in the form.
以下是代码摘录:
# 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
字段)一样,但它仍显示在表单上
This seems like it should be pretty simple, and I know I've done it successfully before. I've deleted/recreated my DB and cleared my browser cache, just to be sure that's not a factor. I also tried making it a HiddenInput
field, just like some_other_field
(which is a ForeignKey
field also), but it still shows up on the form.
这里缺少什么吗?uni_form是否以某种方式覆盖设置?如果没有,关于我可能在调试中寻找什么的任何建议,以查看发生的原因/原因?
Is there something here I'm missing? Is uni_form overriding the setting somehow? If not, any advice as to what I might look for in debug to see where/why this is happening?
(使用Django版本1.2.7)
推荐答案
排除需要一个元组,所以您需要
exclude needs a tuple, so you need
# note the extra comma
exclude = ('created_by',)
django遍历 exclude
,并且由于字符串是可迭代的(返回每个字符),因此不会引发错误
django iterates through the exclude
, and since strings are iterable (return each character) this doesn't throw an error
这篇关于为什么我的排除字段仍然以这种Django形式出现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!