I have a doubt, I want to put a bootstrap class in a select in Django, only that although in forms all the classes are accepting them less 'tecnico'
, in other parts of the code I also have Selects but in this case it is not seeing it, I don't know if it is because I'm using
def __init__
Does anyone have any suggestions how I can add the class "form-select"
to technical
forms.py
class PresupuestosManoObraForm(forms.ModelForm):
class Meta:
model = ManoObra
fields = ['codigo','tecnico','horas','minutos']
exclude = ['estimate_ids']
widgets = {
'codigo': forms.NumberInput(
attrs={
'class': 'form-control',
'id': 'id__manoobra-0-codigo',
}
),
'tecnico': forms.Select(
attrs={
'class': 'form-select',
'id': 'id__manoobra-0-tecnico',
}
),
'horas': forms.NumberInput(
attrs={
'class': 'form-control',
'id': 'id__manoobra-0-horas',
}
),
'minutos': forms.NumberInput(
attrs={
'class': 'form-control',
'onchange': 'convMinHr()',
'id': 'id__manoobra-0-minutos',
}
),
}
def __init__(self, *args, **kwargs):
super(PresupuestosManoObraForm, self).__init__(*args, **kwargs)
technicans = Tecnicos.objects.all()
technico = [(i.nombreTecnico,i.apellidoTecnico) for i in technicans]
self.fields['tecnico'] = forms.ChoiceField(choices=technico)
The problem is that in
__init__
you are overwriting thefield
on this line:self.fields['tecnico'] = forms.ChoiceField(choices=technico)
So the widget you assigned to it before doesn't use it for anything anymore.
One option you have is to put it like this in
__init__
, removing the setting for this field in theMeta
: