How to populate a dropdown in Django?
<p>I want to build a simple view using Django. There will be a dropdown of 'customer site's and then, after one customer site is selected the view will populate a dropdown of competitors of previously selected customer. My trouble is with the second part: when I select the customer from the first dropdown and press 'choose' the page reloads but the competitors dropdown is empty:</p>

<p>I debugged the code and there are values returned for the competitors dropdown and they are correct.
I have this single view:</p>

<pre><code>def index_unify_brands(request):
    class CustomerSiteDropDown(forms.Form):
        customer_site_id = forms.ChoiceField(Site.objects.filter(site_type='customer').exclude(
            crawl_groups='').values_list('id', 'site_name').order_by('site_name'), required=True)

    class OptionsForm(forms.Form):

        competitors_site_ids = forms.ChoiceField()

        def populate_competitors_in_dropdown(self,customer_site_id):
            competitors_ids = get_competitors_positions(customer_site_id)[
                customer_site_id].keys()  # todo: if no competitors

            print competitors_ids
            competitors_for_dropdown = Site.objects.filter(id__in=competitors_ids).exclude(crawl_groups='') \
                .values_list('id', 'site_name').order_by('site_name')
            print competitors_for_dropdown
            self.competitors_site_ids = forms.ChoiceField(competitors_for_dropdown, required=True)

    if request.method == 'POST':
        # received request for a specific customer site id
        customer_dropdown_form = CustomerSiteDropDown(request.POST)
        options_form = OptionsForm(request.POST)
        options_form.populate_competitors_in_dropdown(int(request.POST['customer_site_id']))
        if customer_dropdown_form.is_valid():
            return render_to_response('manage/unify_brands/unify_brands.html',
                                      {'customer_dropdown_form': customer_dropdown_form,
                                       'options_form': options_form},
                                      context_instance=RequestContext(request)
                                      )
    else:

        customer_dropdown_form = CustomerSiteDropDown()
        return render_to_response('manage/unify_brands/unify_brands.html',
                                  {'customer_dropdown_form': customer_dropdown_form},
                                  context_instance=RequestContext(request))
</code></pre>

<p>It seems that when I instantiate the <code>OptionsForm</code> object, the <code>options_form.populate_competitors_in_dropdown(....)</code> method doesn't change the dropdown inside this object that is represented by <code>competitors_site_ids</code> variable.</p>

<p>Any idea what I do wrong here?</p>

<p>In your form, <code>self.competitors_site_ids</code> isn't anything relevant. You need to assign to <code>self.fields['competitors_site_ids']</code>.</p>

<p>Also this code should probably be in <code>__init__</code>, not a separate method.</p>

python django