oTree Forum >

participant level variable

#1 by Andrea

Hello!

I created the following variable:

class MyPage(Page):
    form_model = 'player'
    # names must correspond to fields in models.py
    form_fields = ['choice', 'guess_choice_other']

    @staticmethod
    def before_next_page(player, timeout_happened):
        # Save the player's choice in participant.vars
        participant=player.participant
        participant.choice = player.choice
        
        
with choice being 

choice = models.BooleanField(
        label="Please Choose the Game",
        choices=[
            [True, "Game 1"],
            [False, "Game 2"],
        ],
    )        
    
    
    
Now, I would like to generate a variable (to have different treatments) in a new app as follows:

In the player section in models:

 is_treatment_PD=models.BooleanField()         
 
 
while as a function:

    def creating_session(subsession):
        for player in subsession.get_players():
            # Set is_treatment_PD based on the participant's choice
            player.is_treatment_PD = True if player.participant.vars.get('choice') == True else False


However the variable is not stored. Why? I already created the 'choice' in settings.py.

Many thanks!

#2 by woodsd42

Creating session is run when the session is created - i.e. right at the start.  This doesn't depend on app order or anything like that - when the session is created it runs creating_session for all apps. So, the subject hasn't made the decision yet, and the before_next_page() code has not run at the time of creating session, which I believe is why you're having this issue.  Try putting it in a before_next_page() in the second app (you might need to have a 'dummy' page where they just click Next to facilitate that).

#3 by Andrea

I see! Thank you.
What about that (suggested by Otree):

class ShuffleWaitPage(WaitPage):

    wait_for_all_groups = True

    @staticmethod
    def after_all_players_arrive(subsession):
        subsession.group_randomly()
    etc...
    
 Because it doesn't work for me..

Write a reply

Set forum username