oTree Forum >

Problem with {form_field}_choices()

#1 by manuela,

Hi,

Problem: {form_field}_choices() does not work for me. I always receive an error message. Can you help me find out what is going wrong?


Context:
I plan to dynamically assign choice values to my multiple-choice questions.

Thus, I have been following these instructions: https://otree.readthedocs.io/en/latest/forms.html?highlight=formfield%20label#formfield
It didn't work with my specific MCQ but I can also not make the example from the otree documentation work:

__init__.py
from otree.api import (
	models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer
)

class Player(BasePlayer):
    fruit = models.IntegerField(widget=widgets.RadioSelect, blank=True)

    def fruit_choices(player):
        import random
        choices = ['apple', 'kiwi', 'mango']
        random.shuffle(choices)
        return choices

# PAGES
class Page1(Page):
    form_model = "player"
    form_fields = ["fruit"]

page_sequence = [Page1]

Page1.html
{{ block title }}
    Page title
{{ endblock }}
{{ block content }}

    {{ formfields }}
    {{ next_button }}

{{ endblock }}
    
Once I run otree devserver and open this game, the following error occurs: 
 Application error (500)
Exception: Field uses a radio/select widget but no choices are defined


oTree version: 
I tried this with oTree version 5.8.4 and 5.7.2.

#2 by ccrabbe,

Hi Manuela -

This is a subtle one, I think.  I've done this before.  You're defining your fruit_choices() as a method of your Player class, where in the documentation it's just a function outside of the Model classes:

https://otree.readthedocs.io/en/latest/forms.html?highlight=formfield%20label#field-name-choices

You just need to un-indent that whole method once and it will probably work.

Thanks,
--Chris

#3 by manuela,

Yes, that did the trick. 
Thank you so much, Chris!

Write a reply

Set forum username