In Django custom MultiWidget how can get sub-widgets ids

424 views Asked by At

In Django how to can get ids of sub-widgets that added to custom MultiWidget, for example if I want to attach a JavaScript code to rendered widgets how can I do it?

1

There are 1 answers

0
M.javid On

In regular django custom widgets pattern of auto id has two parts that separated by underscore id_widget-name but in custom MultiWidget all sub-widgets id pattern contains three parts id_widget-name_widget-index, and we can make widgets auto-id similar below:

class MyMultiWidget(forms.MultiWidget):
    ...
    def render(self, name, value, attrs=None):
        ids = ['id_%s_%d' % (name, index) for index in range(len(self.widgets))]
        ...