#1 by mcfi (edited )
Hi! I have created a player variable that assigns randomly either 1 or 0 to each player as follows: class Player(BasePlayer): number = random.choices([1,0], weights=(1, 100), k=1) Since this is a non-model field it is not saved in the database. I have attempted to do different things to save it but have been unsuccessful until now. Does anyone know the easiest way to do this?
#2 by mcfi
Okey, I just found something that works! See below. Create an empty model in the player class. class Player(BasePlayer): number = models.IntegerField() Then define what it consists of using before_next_page(): class First_Page(Page): form_model = 'player' form_fields = ['var1'] @staticmethod def before_next_page(player, timeout_happened): import random player.number = random.choices([1,0], weights=(1, 100), k=1)
#3 by sergio_sapina
Hi mcfi. Your solution will work fine but looking at what you originally wanted to do I recommend using the creating session function so it is assigned when you create the session instead: def creating_session(subsession): for player in subsession.get_players(): # import random # Not best place to import player.number = random.choices([1,0], weights=(1, 100), k=1) This function does not go into any class so no tabs for it, just paste it at the end of your code and it will be executed when you create every session and you will have the values before the experiment is started. Also I would import random at the start of the document instead. It will probably do not matter but you may need random at other places too.