简体   繁体   中英

Django Form Error: Select a valid choice. 29 is not one of the available choices

Form HTML

<form action="" method="post" class="form-horizontal"><div style="display:none"><input type="hidden" name="csrfmiddlewaretoken" value="6b3d58df7bd4f6d10975462aaf3bd42d"></div>
        <input type="hidden" name="paper" value="5225" id="id_paper"><fieldset><div id="div_id_priority" class="control-group">
    <label class="control-label" for="id_priority">Priority</label>
    <div class="controls">
        <select name="priority" id="id_priority">
<option value="1" selected="selected">Primary</option>
<option value="2">Secondary</option>
</select>


        <p class="help-block"><span class="help_text">Primary - 1st Choice. Secondary - 2nd Choice.</span></p>

    </div>
</div> <!-- /clearfix -->
<div id="div_id_topic" class="control-group">
    <label class="control-label" for="id_topic">Topics</label>
    <div class="controls">
        <select name="topic" id="id_topic">
<option value="6">A</option>
<option value="7" selected="selected">B</option>
<option value="9">C</option>
</select>


    </div>
</div> <!-- /clearfix -->
<div id="div_id_subtopic" class="control-group error">
    <label class="control-label" for="id_subtopic">SubTopics</label>
    <div class="controls">
        <select name="subtopic" id="id_subtopic">
<option value="29">KEEPER</option></select>

        <span class="help-inline">Select a valid choice. 29 is not one of the available choices.</span>


    </div>
</div> <!-- /clearfix -->
</fieldset>

        <div class="form-actions">
            <button type="submit" class="btn btn-primary">Add</button>
        </div>

    </form>

VIEW

@login_required
@event_required
def add_topic(request, paper_id):
    event = request.event
    paper = get_object_or_404(SubmissionImportData, pk=paper_id)
    form = TopicForm(request.POST or None, event=event, paper=paper)
    print request.POST  # i see subtopic here
    print form.errors
    if request.method == 'POST' and form.is_valid():
        cd = form.cleaned_data
        subtopic = request.POST.get('subtopic')
        if subtopic:
            subtopic_obj = get_object_or_404(SubTopic, pk=subtopic)
        else:
            subtopic_obj = None

        paper_topic = PaperTopic.objects.get_or_create(
                submission_import_data=paper,
                priority=cd['priority'],
                topic=cd['topic'],
                sub_topic=subtopic_obj)[0]

        msg = 'Topic Successfully Added'
        messages.success(request, msg)
        url = reverse('submissions_nonadmin_view_topic',
                args=[event.slug, paper.id])
        return redirect(url)

FORM CLASS

class TopicForm(BootstrapForm):
    topic = forms.ModelChoiceField(label='Topics',
            queryset=None, required=False, empty_label=None)
    subtopic = forms.ChoiceField(label='SubTopics',
            widget=forms.Select(attrs={'disabled': 'disabled'}),
            required=False)
    paper = forms.IntegerField(widget=forms.HiddenInput())
    priority = forms.ChoiceField(label='Priority',
            choices=PaperTopic.PRIORITY, required=False)

    class Meta:
        fields = (
                'priority', 'topic', 'subtopic', 'paper',
                )
        layout = (
                Fieldset('', 'priority', 'topic', 'subtopic', 'paper',),
                )

    def __init__(self, *args, **kwargs):
        event = kwargs.pop('event')
        #paper_topic = kwargs.pop('paper_topic')
        paper = kwargs.pop('paper')
        super(TopicForm, self).__init__(*args, **kwargs)
        self.fields['paper'].initial = paper.id
        self.fields['topic'].queryset = Topic.objects.\
                filter(setting=event.setting)
        self.fields['priority'].help_text = 'Primary - 1st Choice. Secondary - 2nd Choice.'
        #if paper_topic:
            #self.fields['topic'].initial = Topic.objects.\
                    #get(pk=paper_topic.topic.id)

    def clean(self):
        '''
        Limit topic associations to 2
        '''
        print 111, self.cleaned_data  # subtopic field is missing here
        cleaned_data = self.cleaned_data
        topic = cleaned_data.get('topic', None)
        subtopic = cleaned_data.get('subtopic', None)
        paper = cleaned_data.get('paper', None)
        priority = cleaned_data.get('priority', None)
        paper_obj = get_object_or_404(SubmissionImportData, pk=paper)

        if topic:
            topic_count = PaperTopic.objects.\
                    filter(submission_import_data=paper_obj).count()
            if topic_count >= 2:
                raise forms.ValidationError("You can only select up to two sets of topic and subtopic associations.")

        if PaperTopic.objects.filter(submission_import_data=paper_obj,
               priority=priority).exists():
            raise forms.ValidationError("You have already chosen that priority level.")
        if PaperTopic.objects.filter(submission_import_data=paper_obj,
               topic=topic, sub_topic=subtopic).exists():
            raise forms.ValidationError("You have already chosen that set of Topic and Subtopic association.")
        print 999999000000
        return cleaned_data

When I'm trying to submit my form, I'm getting this error: <span class="help-inline">Select a valid choice. 29 is not one of the available choices.</span> <span class="help-inline">Select a valid choice. 29 is not one of the available choices.</span> .

I am generating the options for the subtopic dropdown list dynamically via AJAX based on the chosen value in topic.

I am able to get see subtopic in my request.POST but when it gets to the clean method, the subtopic field disappears.

I'm not too sure what's going on..

UPDATE

Another thing, when there are no values for subtopic, the select element is set to disabled=disabled . When I try to submit my form like this, I am able to get subtopic field in my clean method. Whereas when the field is not disabled, I am not able to get it in my clean method. That's like the opposite behaviour of what I'm expecting..

ChoiceField() needs a list of choices to validate against to. Although you do this for topic

self.fields['topic'].queryset = Topic.objects.\
                filter(setting=event.setting)

subtopic is initialized as an empty list, thus no option will be valid.

You'll have to initialize the self.fields['subtopic'].choices with every possible subtopic instance and decide with Javascript which subtopics will be shown/hidden(depending on which topic is selected).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM