Django Form - Passing Values
<p>I have the following form and can't seem to use the "self.currentSelectedTeam1" as the initial value for team1 as can be seen at the bottom.  When I do try this I get the following error:</p>

<pre><code>team1 = forms.ModelChoiceField(queryset=StraightredTeam.objects.none(), empty_label=None,initial=self.curentSelectedTeam1)
NameError: name 'self' is not defined
</code></pre>

<p>Form:</p>

<pre><code>class SelectTwoTeams(BootstrapForm):

    def __init__(self, *args, **kwargs):
        user = kwargs.pop('user', None)
        self.curentSelectedTeam1 = kwargs.pop('curentSelectedTeam1', None)
        self.curentSelectedTeam2 = kwargs.pop('curentSelectedTeam2', None)
        super(SelectTwoTeams, self).__init__(*args, **kwargs)
        queryset = StraightredTeam.objects.filter(currentteam = 1)

        self.fields['team1'].queryset = queryset
        self.fields['team2'].queryset = queryset

    team1 = forms.ModelChoiceField(queryset=StraightredTeam.objects.none(), empty_label=None,initial=self.curentSelectedTeam1)
    team2 = forms.ModelChoiceField(queryset=StraightredTeam.objects.none(), empty_label=None,initial=self.curentSelectedTeam2)
</code></pre>

<p>Many thanks in advance for any help, Alan.</p>

<p>This doesn't work becasue the fields <code>team1</code> and <code>team2</code> are created before <code>__init__</code> is called because they're instance members.</p>

<p>This means that <code>self.curentSelectedTeam1</code> and <code>self.curentSelectedTeam2</code> aren't available yet and are most likely <code>None</code>.</p>

<p>In order to set initial values on the form, you need to do so when you instantiate the form itself.</p>

<pre><code>team1_pk = 0
team2_pk = 1

form = SelectTwoTeams(initial={'curentSelectedTeam1': team1_pk, 'curentSelectedTeam2': team2_pk})
</code></pre>

<p>You'll need to either remove your current <code>__init__</code> override or call <code>super()</code> to allow the initial values to be passed to the form's default constructor.</p>

python django