Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
229 views
in Technique[技术] by (71.8m points)

javascript - django chain select

django-smart-selects is an elegant solution that enables Django developers to have chained select inputs in their forms. But as reported by developers in its list of issues, it does not work well when used in formsets.

The javascript it renders is tied to the id of the "chained field" right in the model definition.

How do you think this can be made to work when used in formsets? I think the render method of the widget (please see the django-smart-selects code) needs to somehow be made aware of formsets (and ideally, form prefixes). But how?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I tried this and it works --- In the widgets.py file of django-smart-selects code, simply copy the section that deals with rendering javascript and delete it from the file. Remove the line that concatenates this (now deleted) javascript to the output.

For your formsets, do the complete rendering yourself. That is, in your template don't just do {{ form }} or {{ formset }} or whatever, but explicitly lay out all individual fields by doing {{ form.field }} in a {% for form in formset.forms %} loop.

Paste the javascript code that you copied over from widgets.py and customize it with the IDs in the javascript with {{ form.field.html_name }}

Write in your own customized urls in the javascript, following the pattern in the code.

Voila!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...