pythondjangodjango-formsdynamic-forms

Django Select a valid choice.[...] is not one of the available choices. in a dynamically generated form


I am making a quiz application and I want to make a dynamic form to render the questions.

I use two widgets in my questions (widgets.RadioSelect and widgets.CheckboxSelectMultiple) to render the question's choices. when I submit the form I get the following error:

Select a valid choice.['option1', 'option2'] is not one of the available choices.

rises only from questions with the second widget eg:widgets.CheckboxSelectMultiple. The RadioSelect submits successfully.

forms.py:

    class QuestionForm(forms.Form):
            
        def __init__(self, fields, *args, **kwargs):
    
            super(QuestionForm, self).__init__(*args, **kwargs)
    
            # Init form fields
            for field in fields:
                self.fields[field['name']] = forms.ChoiceField(
                    label=field['label'],
                    choices=field['choices'],
                    widget=getattr(widgets, field['widget']),
                    required=False
                )

views.py:

def quiz(request, quiz_id):

    quiz = get_object_or_404(QCM, pk=quiz_id)

    if request.method == 'POST':
        if request.POST['action'] == 'Save':
            form = QuestionForm(data=request.POST)
            if form.is_valid():
                print('form is valid :)')
                form.save()
            else:
                print('form is not valid :(')
    else:
        form = QuestionForm()

    context = {
        'form': form,
    }
    return render(request, 'quiz/quiz.html', context)

quiz.html

{% extends "quiz/first.html" %}
{% load staticfiles %}

{% block main %}
    <form method="POST" class="form-horizontal" id="qcm_form" enctype="multipart/form-data">
        <div class="row">
            <div class="col-md-12">
                {% csrf_token %}
                
                {% for field in form %}
                    <div class="form-group">
                        <label class="field-label" for="id_{{ field.name }}">{{ field.label }}{% if field.field.required %} <span class="text-danger">*</span>{% endif %}</label>
                        {{ field }}
                    </div>
                {% endfor %}
            </div>
        </div>
        <input type="submit" class="btn btn-primary" name="action" value="Save">
    </form>
{% endblock main %}

Any help would be much appreciated.


Solution

  • The problem was in the forms.Field subclass I used (ChoiceField) witch accepts only string values not lists. And that explains why radio buttons work because they submit a string value and the CheckboxSelectMultiple didn't because it submits a list.

    I have modified my generated fields list to include the forms.Field subclass as well.

    When I have multiple values I use forms.MultipleChoiceField and if it's just one value I assign forms.ChoiceField.