Saving data when there is no field

176 views
Skip to first unread message

Zia

unread,
Mar 3, 2022, 6:06:13 PM3/3/22
to oTree help & discussion
Hi All,

I am not able to find a solution for the following scenario after looking much on oTree. If you can provide any guidance, I would be much grateful.

I have the following field on a Practice.html page

form_model = 'player'
form_fields = ['unitA1']

1- Is it possible that based on a participant's input value for 'unitA1' field, I can do a new calculation within the page (e.g., value_unitA = unitA1 * 5), and then store "value_unitA" in the database?

2- I also want to access "value_unitA" on Practice.html and refer to it inside a sentence. Additionally, I want to access "value_unitA" on a new page, Practice2.html

Both Practice.html and Practice2.html pages are part of one round, and PLAYERS_PER_GROUP = None

Thanks a ton!
Zia


Chris @ oTree

unread,
Mar 3, 2022, 6:11:23 PM3/3/22
to Zia, oTree help & discussion
See inline:

Sent from my phone

On Mar 4, 2022, at 7:06 AM, Zia <ahmadzi...@gmail.com> wrote:

Hi All,

I am not able to find a solution for the following scenario after looking much on oTree. If you can provide any guidance, I would be much grateful.

I have the following field on a Practice.html page

form_model = 'player'
form_fields = ['unitA1']

1- Is it possible that based on a participant's input value for 'unitA1' field, I can do a new calculation within the page (e.g., value_unitA = unitA1 * 5), and then store "value_unitA" in the database?

before_next_page


2- I also want to access "value_unitA" on Practice.html and refer to it inside a sentence. Additionally, I want to access "value_unitA" on a new page, Practice2.html

With JavaScript. There is an example in otree-snippets called “input calculation” or something similar


Both Practice.html and Practice2.html pages are part of one round, and PLAYERS_PER_GROUP = None

Thanks a ton!
Zia


--
You received this message because you are subscribed to the Google Groups "oTree help & discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to otree+un...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/otree/6354f604-1df8-4502-ae93-dbd47288b830n%40googlegroups.com.

Zia

unread,
Mar 3, 2022, 11:15:41 PM3/3/22
to oTree help & discussion
Hi Chris,

Thanks. I was able to read "input calculation" snippet. I have a version of that in my app already. 

What I really want is to perform a calculation in the backend based on a participant's input to a form_field, and to be able to save that calculation in the Data table like a form_field. I tried "before_next_page" but that requires me to have a form_field, but I don't want a form_field for my specific calculation. 

Thanks a ton!
Zia 

Chris @ oTree

unread,
Mar 4, 2022, 4:38:59 AM3/4/22
to Zia, oTree help & discussion
Why don’t you want a form field?

Sent from my phone

On Mar 4, 2022, at 12:15 PM, Zia <ahmadzi...@gmail.com> wrote:

Hi Chris,

Zia

unread,
Mar 4, 2022, 7:22:29 AM3/4/22
to oTree help & discussion
Hi Chris,
I do want a form field for the calculation that I described (let's call it formfieldX). However, if I define formfieldX, I need to include it in the html page and a user would have to fill it. But I don't want the user to fill this formfieldX. All I want is that the backend calculation based on user's input to formfieldY to be stored in formfieldX.  I wasn't able to work it out. Do you think this is possible?

Thanks,
Zia

Chris @ oTree

unread,
Mar 4, 2022, 7:29:32 AM3/4/22
to Zia, oTree help & discussion
I don’t know but what I am saying is to do something similar to the bmi_calculator app in otree-snippets. 

Sent from my phone

Chris Crabbe

unread,
Mar 4, 2022, 11:45:35 AM3/4/22
to Zia, oTree help & discussion
Hi Zia -

You can define as many fields inside the Subsession, Group, and Player classes as you want, and none of them *need* to be included in a Page's form_fields list.  And every model field you define will be included in the default outputs.

So to do what you're asking, I would define formfieldX and formfieldY as FloatFields in the Player model, and then include formfieldY in your Page's form_fields.  Also in that Page class, I'd create a before_next_page method which sets the value of formfieldX to be 3*formfieldY.  The before_next_page method will be run immediately after the subject submits their value of formfieldY, so formfieldX will get set right after they submit the value of formfieldY.

Good luck,
--Chris



Zia

unread,
Mar 4, 2022, 4:50:39 PM3/4/22
to oTree help & discussion
Thank you Chris and Chris!!

I was able to resolve my problems based on your suggestions. The "BMI calculator" snippet was super helpful. And I have a better understanding of how formfields work in oTree. Great!!

Zia

Reply all
Reply to author
Forward
0 new messages