【发布时间】:2013-03-05 20:50:55
【问题描述】:
我正在使用django-crispy-forms 来渲染django 中的模型形式。 django-crispy-forms 已经很好地与Twitter Bootstrap 集成,并为AppendedText 小部件定义了一个布局对象:
我的 django 中有以下模型 models.py
class Historia(models.Model):
persona = models.ForeignKey('Persona',
blank=False,
null=True,
related_name='historias')
fecha = models.DateTimeField(unique=True,
auto_now_add=True)
motivo = models.TextField()
peso = models.DecimalField(blank=True,
max_digits=5, decimal_places=2)
talla = models.IntegerField(blank=True)
tension = models.CharField(max_length=15)
pulso = models.IntegerField()
diagnostico = models.TextField()
tratamiento = models.TextField()
pendiente = models.TextField(blank=True)
我在forms.py 中定义了以下modelform:
class HistoriaForm(django.forms.ModelForm):
class Meta:
model = models.Historia
def __init__(self, *args, **kwargs):
super(HistoriaForm, self).__init__(*args, **kwargs)
self.helper = FormHelper(self)
self.helper.form_class = 'form-horizontal'
self.helper.form_tag = True
self.helper.form_id = 'nueva_historia_form'
self.helper.form_action = 'nueva_historia'
self.helper.form_show_errors = True
self.helper.error_text_inline = False
self.helper.add_input(Submit('crear', 'Crear'))
我的问题是如何更改 peso 字段的小部件,而不必创建新的 widget 子类,因为 django-crispy-forms 已经定义了一个布局对象。另请注意,我不想枚举所有其他字段以便这样做:
self.helper.layout = Layout(
Fieldset(
'Legend',
'nombre',
'apellido',
AppendedText('peso', 'kg')
),
ButtonHolder(
Submit('submit', 'Submit', css_class='button white')
)
)
因为如果我决定在模型中添加标准小部件已经非常有用的新字段,我将不得不继续编辑辅助布局。
编辑:我还看到了在 self.helper.layout[0] = AppendedText('peso', 'kg') 之类的帮助器中访问布局的方法,但我不知道我想更改的字段的索引,它会更好如果我能写self.helper.layout['peso'] = AppendedText('peso', 'kg')
【问题讨论】:
标签: python django django-crispy-forms