我有模特:
class TestPolja(models.Model):
class Meta:
verbose_name = 'Test polja'
integerPolje1 = models.IntegerField('integerPolje1')
charPolje1 = models.CharField('charPolje1',max_length = 1024)
bigint1 = models.BigIntegerField('bigint1',default = 67573737)
date = models.DateField('date')
和modelForm:
class TestPoljaForm(ModelForm):
class Meta:
model = TestPolja
如何更改 TestPoljaForm
添加class
归属于 <input>
标记?
如何添加小部件?
最佳答案
你可以在 __init__
表单方法中做到:
class TestPoljaForm(ModelForm):
class Meta:
model = TestPolja
def __init__(self, *args, **kwargs):
super(TestPoljaForm, self).__init__(*args, **kwargs)
# for example change class for integerPolje1
self.fields['integerPolje1'].widget.attrs['class'] = 'SOMECLASS'
# you can iterate all fields here
for fname, f in self.fields.items():
f.widget.attrs['class'] = 'SOMECLASS'
关于django - 如何将类属性添加到 ModelForm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20614043/