oTree:如何从model.py中的播放器类访问播放器的ID?

时间:2016-08-24 13:57:40

标签: python django otree

我想通过播放器的默认属性id_in_group定义参数变量。但是,这个属性似乎无法通过我能想到的方式访问(例如通过BasePlayer.id_in_group)。

班主任的代码:

class Player(BasePlayer):
    investment_amount = models.CurrencyField(
        doc="""
        Amount invested by this player.
        """,
        min=0, max=Constants.endowment
    )
    random_number = BasePlayer.id_in_group

    def set_payoffs(self):
        for p in self.get_players():
            p.payoff = 110

我如何访问属性id_in_group?或者它是不可能的,因为它是由oTree预设的默认属性?

2 个答案:

答案 0 :(得分:2)

in oTree the id_in_group attribute is assigned after the session started. It is quite logical because the groups haven't been formed yet before the session started.

You are trying to assign a value in the Player class declaration, when none of players have been assigned to groups, thus no one has his/her id_in_group.

Moreover, you assign a value to a property of the model, not to the fields of specific instances (i.e. players). If you need to assign to random_id field of each Player a specific value based on his/her id in group do the following:

in models.py:

class Player(BasePlayer):
     random_id = models.IntegerField()

class Subsession(BaseSubsession):
     def before_session_starts(self):
         for p in self.get_players():
             p.random_id = p.id_in_group 

答案 1 :(得分:-1)

您是否尝试通过super.id_in_groupself.id_in_group访问变量?如果它是父类的变量,则不会像现在这样直接调用该类。

此外,谷歌有很多关于如何访问父类变量的结果,其中很多是来自stackoverflow的答案,可以很好地解释: - )