Handling POST request when using ModelChoiceView
<p>My simple form looks like this:</p>

<pre><code>class PropertyFilterForm(forms.Form):
    property_type = forms.ModelChoiceField(queryset=Property.objects.values_list('type', flat=True).order_by().distinct())
</code></pre>

<p><code>property_type</code> returns a flat list of String values to the drop-down list in my template. </p>

<p>Now, when i choose one of the values and hit "Submit" - i get the following error:</p>

<blockquote>
  <p>Select a valid choice. That choice is not one of the available
  choices.</p>
</blockquote>

<p>My view at the moment looks like this:</p>

<pre><code>def index(request):
    if request.method == 'POST':
        form = PropertyFilterForm(request.POST)
        if form.is_valid():
            selected_type = form.cleaned_data['property_type']
            properties = Property.objects.filter(type=selected_type)
    else:
        form = PropertyFilterForm()
        properties = Property.objects.all()
    return render(request, 'index.html',  context=locals())
</code></pre>

<p>I read and re-read <a href="https://stackoverflow.com/questions/10788777/modelchoicefield-gives-an-invalid-choice-error-upon-form-submission">this question</a> many times. It seems to be the same thing, but still I wasn't able to figure out the exact solution.</p>

<p>What I understood so far, is that I need to either explicitly specify a queryset for my form each time I call it in the view, or (better) specify the queryset in the init method of my form.</p>

<p>Could please someone elaborate on whether we need to specify a queryset the way i described above? </p>

<p>If yes, why? Haven't we already specified it in the form definition?</p>

<p>I would be really grateful for any code snippets </p>