The form structure seems to generate labels here:
def _id_for_label(self): """ Wrapper around the field widget `id_for_label` class method. Useful, for example, for focusing on this field regardless of whether it has a single widget or a MutiWidget. """ widget = self.field.widget id_ = widget.attrs.get('id') or self.auto_id return widget.id_for_label(id_) id_for_label = property(_id_for_label)
This means that you can simply specify the field widget with the id key to set it as you wish.
foo = forms.CharField(widget=forms.TextInput(attrs={'id': 'foobar'}))
Or override init and set attrs after the form is initialized.
I donβt see how this can disrupt the form, since the django forms structure never knows about HTML identifiers (this data is not transmitted to the server ...)
Yuji 'Tomita' Tomita
source share