How to filter an inlined foreign key based on master model
In an inline form, in the admin interface, I have a foreign key field.
If you look at the following image: http://www.image-share.com/ipng-147-172.html you will notice an engine field (Set to proximity).
What I'd like is to filter what appear in the list (currently track.context.max_media_duration and track.ambient.max_media_duration) bases on the engine selection.
I'd like it to change when the selection is changed, it will also have to mark existing one that has been filtered out for deletion or del开发者_运维技巧ete them.
I don't know where to start to implement such a feature.
Thanks
The easiest approach, in my opinion, would be to do it all as an AJAX callback (e.g. with jQuery). The general code flow might be the following:
- Add a jQuery onChange event to the
id_engine
field. - When the
id_engine
pull down changes, it triggers a callback. - That callback calls a URL you have set up back to a specific URL and returns values as JSON data.
- These values are what you use to overwrite what is found in the pull downs below... jQuery can overwrite these pretty simply. You just have to be careful to match what Django outputs by default -- keeping form names and values similar so Django will know how to handle it when POSTing the data back.
精彩评论