1

I have a form composed of 2 ChoiceField and a CharField. The second ChoiceField is populated via ajax when an item of the first one is selected.

The template populating it is the following :

{% for a in assets %}
<option value="{{ a.asset_id }}">{{ a.asset_name }}_{{ a.asset_description }}</option>
{% endfor %}

When I try to submit the form I get the following error :

Select a valid choice. 20 is not one of the available choices.

Pointing to the second ChoiceField, 20 corresponds to the {{a.asset_id}} of the item selected.

I saw this error on other forums but it's almost always due to multipleChoiceField which is not my concern.

What am I doing wrong ?

Johanna
  • 1,343
  • 4
  • 25
  • 44
  • What is the value of `choices` for the ChoiceField in the form? – Sam Starling Aug 24 '11 at 10:49
  • In the form I the value of choice contains `[('', '-- choisir un type en premier')]` at first, then I add some option tags depending on what item was selected in the first CHoiceField – Johanna Aug 24 '11 at 11:35
  • 1
    How do you add the option tags, using Javascript? This is bad, because Django will reject anything selected that isn't in `choices`. Instead, you should dynamically change the value of `choices` in the view. – Sam Starling Aug 24 '11 at 11:46
  • I add them using javascript because I want the items to be updated when I select an item on the first ChoiceField. And I don't know how to do that without javascript. – Johanna Aug 24 '11 at 11:52
  • I implemented that after this discussion : [link](http://chat.stackoverflow.com/rooms/2757/discussion-between-johanna-and-chris-pickett) – Johanna Aug 24 '11 at 11:53
  • I've updated my answer. What Chris said in the chat is 100% correct, but I don't think he knew you were using a ChoiceField. – Sam Starling Aug 24 '11 at 12:14

1 Answers1

6

A ChoiceField needs to have choices set. These don't just determine what is shown in the dropdown, but importantly for you, it also determines what values should be accepted when the form is submitted.

Let me explain what's going on in your situation. You have your choices initially set as:

CHOICES = (
    ('', '-- choisir un type en premier')
)

This means that the only thing will be accepted is a blank choice (the first entry in the tuple, '', is the actual value of the field, and choisir... is the text value).

So when you add <option> elements via Javascript, their values won't be in CHOICES, and so they're getting rejected by Django when you submit that form. 10 isn't in CHOICES.

The view that your form submits to should look at the value of the option submitted to it, and then change the value of CHOICES passed to the ChoiceField accordingly.

Sam Starling
  • 5,298
  • 3
  • 35
  • 52
  • The tuple of choices isn't in that form, I dynamically add tags. I tried to add and remove the quotes around ` – Johanna Aug 24 '11 at 11:35
  • How can I access the choices of the ChoiceField in the view ? – Johanna Aug 24 '11 at 12:38
  • Adding choices in the view processing the form is described [here](http://stackoverflow.com/a/28408178/3922554) – jfn Feb 02 '16 at 10:05