Closed dragon-fire-fly closed 1 year ago
A breakpoint was used to investigate the bug:
class UserEditForm(forms.ModelForm):
p_language_queryset = ProgrammingLanguage.objects.all()
p_language_choices = []
for language in p_language_queryset:
breakpoint()
p_language_choices.append(language.language)
p_language = forms.MultipleChoiceField(choices=p_language_choices, widget=forms.CheckboxSelectMultiple, required=False)
"language" is correctly finding the p_language object (in this case "python").
dir(language)
shows the possible methods for language and ".language" is present.
language.language is correctly displaying "Python"
The problem is that the multiple choice field requires a 2-item tuple - the first item is the database reference and the second is what is displayed in the form. Reference: https://www.geeksforgeeks.org/multiplechoicefield-django-forms/
The following code was added to fix the issue:
class UserEditForm(forms.ModelForm):
p_language_queryset = ProgrammingLanguage.objects.all()
p_language_choices = []
for language in p_language_queryset:
p_language_choices.append((language.language.lower(), language.language))
p_language = forms.MultipleChoiceField(choices=p_language_choices, widget=forms.CheckboxSelectMultiple, required=False)
Which resulted in correct rendering of the checkboxes:
Further bug related to this bug fix : the id (as an integer) of the programming language was expected instead of the name of the language:
this was fixed by changing p_language_choices.append((language.language.lower(), language.language))
to p_language_choices.append((language.id, language.language))
Describe the bug Whilst trying to change the multi-select panel to a series of checkboxes, the names of the programming languages are not displaying correctly.
To Reproduce Steps to reproduce the behavior:
Expected behavior Checkboxes displayed with the name of the programming language next to them
Screenshots Before: Now:
Desktop (please complete the following information):
Additional context This is the current code in app_user/forms.py: