oTree Forum >

passing variables from one app to next?

#1 by rvman0523

I'm trying to figure out how to pass variables from one app to another, but keep hitting an error. I'm using oTree 3

Here's my code:
In settings.py:

PARTICIPANT_FIELDS = ['age','gender','income']

In pages.py in app 1:

class BeginSurvey(Page):
    # deploys class 'player' and display the following questions: age, gender, income
    form_model = 'player'
    form_fields = ['age', 'gender', 'income']

    def before_next_page(self):
# The label and other information will be stored and passed to next application (main application)
            self.participant.vars['age'] = self.player.age
            self.participant.vars['gender'] = self.player.gender
            self.participant.vars['income'] = self.player.income
In pages.py in app 2:

class finalSurvey_playerInfo(Page):
    form_model = 'player'

    def get_form_fields(self):
        if self.participant.gender == 'male':
            return ['var1','var2']

        elif self.participant.gender == 'female':
            return ['var1']
However, I get an error message saying:
AttributeError: 'Participant' object has no attribute 'gender'

Where am I making a mistake?

#2 by Chris_oTree

what version of otree? PARTICIPANT_FIELDS is new in otree 5.

#3 by rvman0523

oTree 3.

#4 by Chris_oTree

Then use participant.vars['gender'], not participant.gender

#5 by rvman0523

Thanks, that worked!

#6 by rvman0523

I have a follow up question. On another page, I display the gender of the self, as well as the partner in the group.

In pages.py:

class finalSurvey_playerInfo(Page):
    form_model = 'player'

    def vars_for_template(self):
            'ego_gender': self.participant.vars['gender'],
            'partner_gender': self.group.get_others_in_group().vars['gender']
 However, I get the following error message:
 AttributeError: 'finalSurvey_playerInfo' object has no attribute 'get_others_in_group'
 What am I doing wrong here?

#7 by rvman0523

I also try the following modified version but it throws out the same error message:

    def vars_for_template(self):
        partner = self.get_others_in_group(), # there is only 1 partner i.e. group size = 2
        partner_gender = partner.participant.vars['gender'],

            'ego_gender': self.participant.vars['gender'],
            'partner_gender': partner_gender

#8 by rvman0523

the following worked:

    def vars_for_template(self):
        partnerid= self.player.get_others_in_group()[0]
        partner_gender = partnerid.participant.vars['gender']
            'gender_display': self.participant.vars['gender'],
            'partner_id': partnerid,
            'partner_gender': partner_gender

Write a reply

Set forum username