How do I get dynamic choices to a select widget at render time?

126 views
Skip to first unread message

Mike Dewhirst

unread,
Feb 19, 2018, 7:17:26 PM2/19/18
to django users
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'
        ]

So how do I get that set of choices into the Answer form?

I can see from the docs that the form Meta class can have a widgets
attribute for the 'answer' field. I think I need a ChoiceField widget so
I can include the choices. I tried using __init__() in the form class to
get the choices on board at instantiation but still couldn't get it
going because class Meta doesn't see 'self'.

Thanks for any help

Stumped

Mike Dewhirst

unread,
Feb 20, 2018, 3:05:05 AM2/20/18
to django users
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

            )

Andy

unread,
Feb 20, 2018, 7:51:12 AM2/20/18
to Django users
use django-select2

Mike Dewhirst

unread,
Feb 22, 2018, 4:45:09 AM2/22/18
to django...@googlegroups.com
On 20/02/2018 11:51 PM, Andy wrote:
> use django-select2

Had a look at the docs and django-select2 doesn't address this use case.
However, I can see I will need it for ORM related selections in the near
term.

Thanks Andy.
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/4eaf732e-54c5-4e43-ba6e-8ef72c999c90%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/4eaf732e-54c5-4e43-ba6e-8ef72c999c90%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages