django: BooleanField, how to set the default value to true? - Stack Overflow
2012-04-07 11:59
1436 查看
django: BooleanField, how to set the default value to true? - Stack Overflow
down vote
acceptedIf you're just using a vanilla form (not a ModelForm), you can set a Field initial value (http://docs.djangoproject.com/en/dev/ref/forms/fields/#django.forms.Field.initial) like
Finally, if you want to dynamically choose at runtime whether or not your field will be selected by default, you can use the initial parameter to the form when you initialize it:
down vote
acceptedIf you're just using a vanilla form (not a ModelForm), you can set a Field initial value (http://docs.djangoproject.com/en/dev/ref/forms/fields/#django.forms.Field.initial) like
class MyForm(forms.Form): my_field = forms.BooleanField(initial=True)If you're using a ModelForm, you can set a default value on the model field (http://docs.djangoproject.com/en/dev/ref/models/fields/#default), which will apply to the resulting ModelForm.
Finally, if you want to dynamically choose at runtime whether or not your field will be selected by default, you can use the initial parameter to the form when you initialize it:
form = MyForm(initial={'my_field':True})
相关文章推荐
- How to set the default input focus on a field in an HTML web form
- How to set default value for a field
- (Django) How do I set limit_choices_to another class from where the ManyToManyField points to? - django, django-models - TechQues.com
- Cognos How to set the default email sender to a specific value
- Here's an example that shows how to enumerate through the features of a selection set, returning the value of each field in the attribute table except for the geometry colum.
- how to change the implicit default settings in django 1.6
- How to modify Inventory Aging Report form days field default value
- How to set the style in valuelist
- [Infopath]使用jquery给infopath表单的的field赋值。 how to set value to Infopath field by Jquery
- HOW TO CHANGE THE DEFAULT KEY-VALUE SEPARATOR OF A MAPREDUCE JOB
- How to set the sequence value while saving the record in OA Framework
- How to Set Today theme to default theme.
- [SharePoint]javascript client object model 获取lookup 类型的field的值,包括user类型(单人或者多人)的值。how to get the multiple user type/lookup type field value by Javascript client object model
- [Drupal] Editing How to modify the CCK date field, set its to-date to null
- How To Use The New Multivalue Field In Access 2007/2010 Sunday, June 19th, 2011
- How to sort the dictionary by the value field
- NHibernate Notes3_How to set a default value of column
- How to chage the default opstion value when adding new products?
- How-to increase the default timeout value of vSphere Web Client
- How to set the DefaultButton in a Page Based on ASP.NET Master Page