On 20/02/2018 11:16 AM, Mike Dewhirst wrote:
> Here is an example of get_choices() output ... it comes from a method
> on the Question model.
>
> [('A', 'A - Once?'), ('B', 'B - Twice?'), ('C', 'C - Four times?'),
> ('D', 'D - Twelve times?'), ('E', 'E - Continously as changes are
> made?')]
>
> It is available before the the answer form is instantiated ...
>
> class AnswerSingleForm(forms.ModelForm):
>
> class Meta:
> model = Answer
> fields = [
> 'answer',
> 'score'
> ]
>
This is my new form which seems to work ...
class AnswerSingleForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(AnswerSingleForm, self).__init__(*args, **kwargs)
answer = kwargs['instance']
choices = answer.question.get_choices()
if len(choices) > 0:
self.fields['answer'] = forms.ChoiceField(
choices=choices
)