【发布时间】:2016-02-26 19:53:50
【问题描述】:
我手动将我的表单集显示为一个表格,每个表单都被循环遍历。在每个表单的底部,我都包含以下隐藏字段:
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
但问题是我的表单中还包含以下属性:
class AllocationForm(forms.ModelForm):
name = forms.CharField(widget=forms.TextInput(attrs={'size': '15'}))
def __init__(self, *args, **kwargs):
super(AllocationForm, self).__init__(*args, **kwargs)
if self.instance:
self.fields['total_budgeted'] = self.instance.total_budgeted()
self.fields['total_budgeted_account_percent'] = self.instance.total_budgeted_account_percent()
self.fields['actual_spent'] = self.instance.actual_spent()
self.fields['actual_spent_account_percent'] = self.instance.actual_spent_account_percent()
self.fields['total_budgeted_category_percent'] = self.instance.total_budgeted_category_percent()
self.fields['actual_spent_category_percent'] = self.instance.actual_spent_category_percent()
class Meta:
model = Allocation
exclude = {'created', 'modified', 'source_account'}
从某种意义上说,这很有效,因为我确实看到了被调用的属性,但是它们显示为空,所以这是另一个问题。
问题是当我将隐藏字段保留在模板中时,我会收到诸如“int”对象没有属性“get_bound_field”等错误,具体取决于属性/方法调用的返回类型。
我的问题是首先:是否可以检查该字段是否是模板中的属性并因此跳过它? 这可能与我如何使用该属性有关,因为实际上每个属性都没有显示任何内容(但我看到它回调),所以 second 将是关于如何显示属性。
【问题讨论】:
标签: django properties hidden-field modelform