select default option django form 1

select default option django form

class MyForm(forms.ModelForm):
    field_name = forms.ModelChoiceField(queryset=Position.objects.all(),
        empty_label="(Select here)")

Here is what the above code is Doing:
1. Create a new form class called MyForm.
2. Create a new field called field_name.
3. Set the field_name field to be a ModelChoiceField.
4. Set the queryset for the field_name field to be all of the Position objects.
5. Set the empty_label for the field_name field to be “(Select here)”.

Similar Posts