From b39268f42d50964e62253493c664c0e87972cb6f Mon Sep 17 00:00:00 2001 From: "AppleGrew (applegrew)" Date: Thu, 28 Feb 2013 21:17:20 +0530 Subject: [PATCH] Issue#26 fix. --- django_select2/widgets.py | 6 +++++- testapp/testapp/testmain/forms.py | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/django_select2/widgets.py b/django_select2/widgets.py index e3c60ad..88955b9 100644 --- a/django_select2/widgets.py +++ b/django_select2/widgets.py @@ -231,7 +231,11 @@ class Select2Widget(Select2Mixin, forms.Select): self.options.pop('multiple', None) def render_options(self, choices, selected_choices): - if not self.is_required: + all_choices = chain(self.choices, choices) + if not self.is_required and \ + len([value for value, txt in all_choices if value == '']) == 0: # Checking if list already has empty choice + # as in the case of Model based Light fields. + choices = list(choices) choices.append(('', '', )) # Adding an empty choice return super(Select2Widget, self).render_options(choices, selected_choices) diff --git a/testapp/testapp/testmain/forms.py b/testapp/testapp/testmain/forms.py index d04a7e8..b4f8b06 100644 --- a/testapp/testapp/testmain/forms.py +++ b/testapp/testapp/testmain/forms.py @@ -116,6 +116,8 @@ class MixedForm(forms.Form): always_fail_rooms_multi = ClassRoomChoices(validators=[validate_fail_always]) always_fail_self_choice = SelfChoices(validators=[validate_fail_always], auto_id='always_fail_self_choice') always_fail_self_choice_multi = SelfMultiChoices(validators=[validate_fail_always], auto_id='always_fail_self_choice_multi') + model_with_both_required_and_empty_label_false = ModelSelect2Field( + queryset=Employee.objects, empty_label=None, required=False) #issue#26 # These are just for testing Auto registration of fields EmployeeChoices() # Should already be registered