Django model choice field - depend on other field's choice
You probably can't because it depends of user interaction with your form: your server can't know in advance which element your user will select before sending the form to the browser. You could probably achieve this using ajax. I think a working process could be :
- Create a form with all the fields, and make
make
field hidden - Create a view (I'll call it
AjaxMakeFieldView
) that will catch an ajax request taking avehicle_type
argument and return the HTML formake field
, populated with relevant data. Add a URL in your URLConf for this view. - In your template, add a Javascript binding : when user select a
vehicle_type
, the browser will send aan ajax request toAjaxMakeFieldView
and replace hiddenmake
field with returned HTML
If you don't want javascript, another way would be a two step form :
- A first form with a
vehicle_type
field - Once the first form is submitted, your user get a second form with a
make
field, which initial data is populated depending ofvehicle_type
selected in the first form.
I've never done this, but Django documentation on Form wizard seems a good place to start.