oTree Forum >

Shuffle choices in dropdown form field

#1 by nizipzi (edited )

Dear All, 

I am new to oTree and I am struggeling since a while with flexibilizing choices in a dropdown menu of a form field. 
More, concretely I would like to shuffle the order of the options in the dropdown menu such that choice options are presented to each player in random order. 

Based on the docs I tried the following in init.py

class Player(BasePlayer):
    institutions = models.StringField()

    def institutions_choices(player):
        choices = ["a", "b", "c", "d"]
        random.shuffle(choices)
        return choices

# PAGES
class SelectInstitution_all(Page):
    form_model = "player"
    form_fields = ["institutions"]

page_sequence = [SelectInstitution_all]

and in html 

{% formfield player.institutions %}

Unfortunately no dropdown form field is returned. Can someone help?

Thanks a lot for your help!

#2 by xuxu1989

Hi, 
I'm not sure which version of otree you are using. If you are using otree<5, you need to move the function institutions_choices to be inside the class SelectInstitution_all. If you are using otree>=5, you need to take the function institutions_choices outside the class Player (delete the indention), and {% formfield player.institutions %} should be {{ formfield player.institutions }}.

Write a reply

Set forum username