django : update comboBox with SQL query
I want to update a combobox with a SQL query to my database depending on what item was selected in another combobox.
This sound 开发者_运维知识库like a quite common problem, but I'm new to django and a bit lost with the connections between the views and templates so I don't know how to attack the problem.
I think I need to use jquery to be able to dynamically update one field, but I'm also new to jquery...
If anyone has a few hints, they would be appreciated!
One approach is to attach on onchange event handler to your first combo box, and fire off an ajax get request, passing in the value of the combo box to a view function.
From here you have a choice. Return a JSON object and handle creating the choices for the next combo box using JavaScript, or return HTML that you'll use to update your template.
If memory serves, there are some django widgets out there that will do this for you, but you won't learn anything if you go that route :)
Getting familiar with jQuery's Ajax, event and manipulation commands and working through a static proof of concept will be the fastest way to go. Happy coding!
精彩评论