When you use a Model Choice Field in Django, it automatically creates an empty option in the generated select widget. But in some special cases, you don’t want this empty option to be there. So I decided to find the way to remove it. It’s much more simple then you think. Just create your model declaring the model choice field like this:
fieldname = forms.ModelChoiceField(queryset=RelationModel.objects,empty_label=None)
It’s simple like that. Just set the empty_label argument to None. But whe using ModelFormSets, there is a little more work to do. You have to create a new BaseModelFormSet like this:
from django.forms.models import BaseModelFormSet</p>
<p>class MyBaseModelFormSet(BaseModelFormSet):
def add_fields(self, form, index):
super(MyBaseModelFormSet,self).add_fields(form,index)
form.fields['fieldname'] = forms.ModelChoiceField(queryset=RelationModel.objects,empty_label=None)
Pay atention that you have to override the add_fields method and change your field in the form fields array. Finally you have to pass this BaseModelFormSet as argument to the modelformset_factory:
from django.forms.models import modelformset_factory</p> <p>MyModelFormset = modelformset_factory(MyModel,formset=MyBaseModelFormSet)
