oTree Forum >

Application error (500) KeyError although the variable is defined in PARTICIPANT_FIELDS

#1 by Mathias

Hi all,

I have defined several keys in PARTICIPANT_FIELDS in settings.py but keep getting the Application error (500) KeyError: 'variable_name' despite settings.py reading

PARTICIPANT_FIELDS = [
    'variable_name'
] 

I am using the latest OTree version, running a local dev server, and need to debug this error. Do you have any pointers?

1) Curiously, print() statements --- even if added to lines in creating_session before I assign any keys --- fail to show up neither in the browser console in the terminal console. 
2) Secondly, I would like to ask if the PARTICIPANT_FIELDS have any restrictions on form or if I can assign any object (e.g. another object holding objects) as PARTICIPANT_FIELD?

Thanks for your help,
cheers
Jana

#2 by Shawn

Hi Jana,

Would you mind sharing with us how this participant variable is coded in your _init_.py?

Shawn

#3 by Mathias

Hi,
yes sure, please point out a comma mistake, or coding error, I'd be very glad, I'm quite sure I must have messed something up. This assigns my participant variable `dfe_condition`

class Subsession(BaseSubsession):  
  def creating_session(subsession):
    if subsession.round_number == 1:
      for p in subsession.get_players():
        participant = p.participant
        # for testing set this to true
        participant.dfe_condition = True
        

(I *don't* use PyCharm but code it in Sublime Text, but this shouldn't matter.)

#4 by Chris_oTree

This code is mixing the old and new oTree formats. creating_session should be a function outside of any class. By the way I recommend taking a look at oTree Studio since it prevents this and many other common errors.

#5 by Furuneko

creating_session() is not a Subsession method, hence it should be placed outside of the class definition.

class Subsession(BaseSubsession):  
  pass
  
def creating_session(subsession):
    if subsession.round_number == 1:
      for p in subsession.get_players():
        participant = p.participant
        # for testing set this to true
        participant.dfe_condition = True
        
This kind of errors are prevented by oTree Studio. I would encourage you to give it a try.

Best

Write a reply

Set forum username