1. Sergey Maranchuk
  2. DjangoBB
  3. Pull requests

Pull requests

#18 Merged
Repository
akava
Branch
default
Repository
slav0nic
Branch
default

Fixed single choice poll logic

Author
  1. Andrei Kavaleu
Reviewers
Description

The reason of this bug is: for multiple choice pools the form uses MultipleChoiceField and CheckboxSelectMultiple widget, but for single choice pools it uses ChoiceField and RadioSelect. RadioSelect+ChoiceField returns only the choice itself, not a list (string id in this case). When we ask len("13"), for example, it returns 2, so validation fails.

The best solution I've found so far is a variation of sedah's solution (see pull request #13), but with a reasonable check (not the "instance of" magic):

if self.poll.choice_count == 1:
    ids = [ids]

I also tried (a dumb idea) to use MultipleChoiceField with RadioSelect widget but them doesn't work together :)

  • Learn about pull requests

Comments (2)

  1. Andrei Kavaleu author

    I also want to extract "choice_count == 1" into Pool's method single_choice(), so the checks (the current one and the one that selects right field type) will look like this:

            if self.poll.single_choice():
                ids = [ids]
    
    # 2nd check:
            if self.poll.single_choice():
                self.fields["choice"] = forms.ChoiceField(
                    choices=choices, widget=forms.RadioSelect
                )
            else:
                self.fields["choice"] = forms.MultipleChoiceField(
                    choices=choices, widget=forms.CheckboxSelectMultiple
                )