【发布时间】:2012-01-31 08:53:08
【问题描述】:
我希望能够在我的 django 表单中提取不同的信息:
这是我的表格:
<form action="" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>
class InstanceForm(ModelForm):
class Meta:
model = models.BaseAsset
widgets = {
'labels': LabelIconCheckboxSelectMultiple()
}
型号:
class AssetClass(models.Model):
default_labels = models.ManyToManyField(Label, null=True, blank=True)
pass
M2M 参考字段
class Label(models.Model):
explanation = models.CharField(null=True, max_length=63)
svgpreview = models.CharField(null=True, max_length=31)
def __unicode__(self):
return unicode(self.explanation)
pass
现在{{ form.as_p }}生成的HTML代码如下:
<li><label for="id_labels_0"><input type="checkbox" name="labels" value="1" id="id_labels_0" /> Consult owner before using</label></li>
<li><label for="id_labels_1"><input type="checkbox" name="labels" value="2" id="id_labels_1" /> This item is broken</label></li>
这意味着它显然使用了模型“标签”的__unicode__ 渲染。如何在 Select 小部件中更改该行为,以便它使用不同的函数来填充它的选择?我正在尝试以合理便携的方式在复选框旁边打印'<img src="{{label.svgpreview}}" alt="{{label.explanation}}"...>'?
【问题讨论】:
标签: django django-forms