Error when defining field choices dynamically in Otree 5.x

190 views
Skip to first unread message

razva...@gmail.com

unread,
Mar 19, 2021, 7:05:32 AM3/19/21
to oTree help & discussion
Hi,

I am trying to dynamically define the choices of a field in Pages.py. This works fine when using Otree 3.x but gives the following exception when running Otree 5.x:  Field uses a radio/select widget but no choices are defined. Thank you very much!

Razvan

Chris @ oTree

unread,
Mar 19, 2021, 7:13:42 AM3/19/21
to oTree help & discussion
Can you show your code? It's working fine for me.

razva...@gmail.com

unread,
Mar 19, 2021, 7:20:54 AM3/19/21
to oTree help & discussion
In models.py:
class Player(BasePlayer):
        choosen_number = models.IntegerField(
        label="Please choose your number for this period.",
        widget=widgets.RadioSelectHorizontal
    )

in pages.py:
class Choose_number(Page):      
    form_model = 'player'
    form_fields = ['choosen_number']
    
    def choosen_number_choices(self):
        choices = [1,2,3] #here I dynamically determine which numbers are available but I don't think that is relevant. I can post if necessary.
        return choices
 
I am running this fine with Otree 3.x but not with Otree 5.x.

Thanks!
Razvan

Chris @ oTree

unread,
Mar 19, 2021, 7:26:50 AM3/19/21
to oTree help & discussion
You should move that method into the Player class; see here: https://otree.readthedocs.io/en/self/misc/version_history.html#new-format-for-form-validation

razva...@gmail.com

unread,
Mar 19, 2021, 8:08:13 AM3/19/21
to oTree help & discussion
Thanks! it works now.
Reply all
Reply to author
Forward
0 new messages