简体   繁体   中英

pass data from javascript to django admin

I have two dropdown list say, "questions" and "choices" in django admin. Depending on the selected option in "question", options in "choices" will get populated. I wrote a javascript that gets triggered on question change, as shown below. To populate "choices", I need to query the database. Can I do that from this javascript function? If yes, how?

Alternatively, if I could return the selected value to the ModelForm class in django admin, I could query database from there. But I am unable to capture this return value in ModelForm and populate "choices" field after user selects an option in "questions". How do I do that?

I could not find any answer on the internet. All I found was sending data from javascript to django views through HttpResponse. This approach also needs a separate regex definition in urls.py. I cannot change the url while I communicate with javascript.

test.js

function tryCustomiseForm() {
    choice=django.jQuery('[id$=choice_list]');
    var selection = null
    django.jQuery('[id$=question_list]').change(function() {
        selection = this.value;
    });
    return selection
}

(function($) {
   $(document).ready(function() {
      tryCustomiseForm();
   });
})(django.jQuery);

我最终使用django-smart-select在models.py中连接我的模型。

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