Added a condition to check whether value is in present in choices for ThemableSelectWidget.

Sometimes value is populated with uuid of resources but that resource is not present in openstack,
So it is good to change initial_value with one of the values from choices.

Closes-Bug: #1928953
Change-Id: Ia33aaf7019d73f96c27e75ee24d80dcbf3e8ceb2
This commit is contained in:
shubhamdang 2021-05-19 20:36:24 +05:30 committed by Shubham Dang
parent 6071b622b2
commit cb8ff87090
1 changed files with 7 additions and 1 deletions

View File

@ -308,6 +308,8 @@ class ThemableSelectWidget(SelectWidget):
new_choices = []
initial_value = value
# Initially assuming value is not present in choices.
value_in_choices = False
for opt_value, opt_label in itertools.chain(self.choices, choices):
other_html = self.transform_option_html_attrs(opt_label)
@ -318,15 +320,19 @@ class ThemableSelectWidget(SelectWidget):
opt_label = self.transform_option_label(opt_label)
# If value exists, save off its label for use
# and setting value in choices to True
if opt_value == value:
initial_value = opt_label
value_in_choices = True
if other_html:
new_choices.append((opt_value, opt_label, other_html))
else:
new_choices.append((opt_value, opt_label))
if value is None and new_choices:
# if value is None or it is not present in choices then set
# the first value of choices.
if (value is None or not value_in_choices) and new_choices:
initial_value = new_choices[0][1]
attrs = self.build_attrs(attrs)