How to show text in Select list of Form EN django type 'placeholder'

0

I would like to add a default text to the select list as well as the 'placeholder' of a TextInput, but I do not get it, here in the form I have the widgets:

'specialty': forms.Select (attrs = {'class': 'form-control', 'placeholder': 'select your specialty'}),

Of course this does not work because it is not the correct way, then how should I do it, is it possible? beforehand, thanks, regards!

    
asked by Demaro Create 22.02.2017 в 14:57
source

1 answer

1

Friend, it is very easy to get what you want, the only thing is that it is not part of any widget, but a special attribute of the ModelChoiceField model, which is a Field ( django.forms.fields.Field ), so you must know how to set it properly. Being a special attribute of ModelChoiceField, it means that it will only work in fields that are relationships with other models, like this:

class Formulario(forms.ModelForm):
    empty_label_message = 'Este seria mi placeholder para un select'

    relation_2 = forms.ModelChoiceField(
       queryset=MyOtherModel.objects.all(), empty_label=empty_label_message)

    class Meta:
       model = MyModel
       fields = ('name', 'relation_1', )

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['relation_1'].empty_label = empty_label_message

Those would be two of the possible ways to add your 'Placeholder' to a select in django.

Any questions, comments.

    
answered by 24.02.2017 в 22:25