Display empty_label on required selectDateWidget on Django
Unfortunately, empty label in SelectDateWidget
is only used if field is not required, but you can simply change this by subclassing SelectDateWidget
and overriding create_select
method:
class MySelectDateWidget(SelectDateWidget):
def create_select(self, *args, **kwargs):
old_state = self.is_required
self.is_required = False
result = super(MySelectDateWidget, self).create_select(*args, **kwargs)
self.is_required = old_state
return result
But in that case you may have to override also validation of your field, so it will throw error that field is required, not that choice is invalid when select is left on blank value.
Although create_select
has been removed in recent versions of Django I was able to use an approach very similar to @GwynBleidD's answer by subclassing SelectDateWidget and overriding get_context.
class MySelectDateWidget(SelectDateWidget):
def get_context(self, name, value, attrs):
old_state = self.is_required
self.is_required = False
context = super(MySelectDateWidget, self).get_context(name, value, attrs)
self.is_required = old_state
return context