TEC
TEC

Reputation: 53

How to create a multiple choice question in oTree?

I was wondering if it is possible to have a multiple choice question in otree. Something like radio button but that lets you choose more than one thing. What I'm thinking of is something like:

Question: The following list of statements contains three correct statements and three false statements. Please select the three correct statements:

Upvotes: 2

Views: 2035

Answers (1)

IonicSolutions
IonicSolutions

Reputation: 2599

You can use otree_models.models.MultipleChoiceFormField for this purpose, as sketched out in the following:

In models.py:

from otree.api import BasePlayer
from otree_tools.models import fields as tool_models

class Player(BasePlayer):

    correct_statements = tool_models.MultipleChoiceModelField(label="Please select the three correct statements",
                                                              min_choices=3, max_choices=3)

In pages.py:

from ._bultin import Page

class ExamplePage(Page):

    form_model = "player"
    form_fields = ["correct_statements"]

    def correct_statements_choices(self):
         """Return the list of statements to choose from."""
         return ["Statement 1", "Statement 2", "Statement 3",
                 "Statement 4", "Statement 5", "Statement 6"]

In ExamplePage.html, simply include the form field:

{% extends "global/Page.html" %}
{% load otree %}

{% block content %}
The following list of statements contains three correct statements and three false statements. 

{% formfield player.correct_statements %}

{% next_button %}

{% endblock %}

Upvotes: 1

Related Questions