Answer the question
In order to leave comments, you need to log in
How to set default form value in django?
Model:
class MyModel(models.Model):
enterprise = models.ForeignKey(Enterprise, verbose_name=_(u'Предприятие'), unique=True)
retry = models.PositiveIntegerField(verbose_name=_(u'Количество попыток'), default=0)
delay = models.PositiveIntegerField(verbose_name=_(u'Задержка между попытками, с'), default=1)
class MyModelForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(MyModelForm, self).__init__(*args, **kwargs)
self.fields['retry'].required = False
#не работает
#self.fields['retry'].initial = 1
#тоже не работает
#delay = forms.IntegerField(label=_(u"title"), initial=1, required=False)
class Meta:
model = MyModel
fields = ('enterprise', 'retry', 'delay')
Answer the question
In order to leave comments, you need to log in
Either define default values in the model
class MyModel(models.Model):
retry = models.PositiveSmallIntegerField(default=1)
...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question