0

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?

Anthon
  • 69,918
  • 32
  • 186
  • 246
M.javid
  • 6,387
  • 3
  • 41
  • 56

1 Answers1

0

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))]
        ...
M.javid
  • 6,387
  • 3
  • 41
  • 56