oTree Forum >

Problem with {form_field}_choices()

#1 by manuela


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?

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:

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']
        return choices

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

page_sequence = [Page1]

{{ 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:


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


#3 by manuela

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

Write a reply

Set forum username