Setting the selected value in Django forms. Choicefield

Here is the field declaration in the form:

max_number = forms.ChoiceField(widget = forms.Select(), choices = ([('1','1'), ('2','2'),('3','3'), ]), initial='3', required = True,) 

I would like to set the initial value to 3 and this doesn't seem to work. I played with the parameter, quotes / without quotes, etc., but without changes.

Can someone give me an exact answer if possible? And / or the necessary settings in my code snippet?

I am using Django 1.0

+102
django forms django-models django-forms field
Mar 18 '09 at 9:57
source share
8 answers

Try setting the initial value when creating the form:

 form = MyForm(initial={'max_number': '3'}) 
+105
Mar 20 '09 at 9:04
source share

This is not an immediate question, but this Q / A is suitable for searches related to trying to assign the selected value to ChoiceField .

If you have already called super().__init__ in your Form class, you should update the dictionary form.initial , not the field.initial property. If you examine form.initial (for example, print self.initial after calling super().__init__ ), it will contain the values ​​for all fields. Having a value of None is that dict will override the value of field.initial .

eg.

 class MyForm(forms.Form): def __init__(self, *args, **kwargs): super(MyForm, self).__init__(*args, **kwargs) # assign a (computed, I assume) default value to the choice field self.initial['choices_field_name'] = 'default value' # you should NOT do this: self.fields['choices_field_name'].initial = 'default value' 
+77
Jul 09 2018-12-18T00:
source share

You can also do the following. in your def class class:

 max_number = forms.ChoiceField(widget = forms.Select(), choices = ([('1','1'), ('2','2'),('3','3'), ]), initial='3', required = True,) 

then when you call the form in your view, you can dynamically set both the initial options and the selection list.

 yourFormInstance = YourFormClass() yourFormInstance.fields['max_number'].choices = [(1,1),(2,2),(3,3)] yourFormInstance.fields['max_number'].initial = [1] 

Note: the initial values ​​should be a list, and the selection should be 2-tuples, in my example above I have a list of 2 tuples. Hope this helps.

+38
Jul 01 2018-10-01T00:
source share

I ran into this problem and found out that the problem is in the browser. When you update your browser, it re-populates the form with the same values ​​as before, ignoring the checked field. If you look at the source, you will see that the checked value is correct. Or place the cursor in the URL field of your browser and press enter. This will reload the form from scratch.

+18
Dec 02 '09 at 15:29
source share
Both Tom and Burton end up answering my questions, but I had a little problem with how to apply them to ModelChoiceField .

The only trick is that the selection is saved as tuples (<model ID>, <model unicode repr>) , so if you want to set the initial selection of the model, you pass the model ID as the initial value, not the object itself or its name or anything else. Then it is as simple as:

 form = EmployeeForm(initial={'manager': manager_employee_id}) 

Alternatively, the initial argument can be ignored instead of an extra line with:

 form.fields['manager'].initial = manager_employee_id 
+6
Feb 27 '11 at 9:45
source share

Dave - good luck finding a solution to your browser problem? Is there a way to force update?

Regarding the original problem, try the following when initializing the form:

 def __init__(self, *args, **kwargs): super(MyForm, self).__init__(*args, **kwargs) self.base_fields['MyChoiceField'].initial = initial_value 
+3
Jan 13 '10 at 21:27
source share

To make sure I need to visualize the form. The initial value is used only in an unrelated form, if it is bound and the value for this field is not included, nothing will be selected.

+1
Mar 18 '09 at 12:06
source share

Try it, it will work for me, I added this to models.py

 options = ( ('1','1'), ('2','2'), ('3','3') ) max_number = forms.ChoiceField(max_length = 2, choices = options, default ='3') 
-3
Apr 24 '15 at 6:03
source share



All Articles