0

I'm trying to create a for loop for creating a dynamic number of objects in Otree, which extends Django (I know, crazy idea, but bear with me). Unfortunately the latest version of otree will throw compile time errors if you attempt to use integers in your code. So for example, the following code:

num_decs = 8
for d in range(1, num_decs + 1):
locals()['dec_r'+str(r)+'_'+str(d)]= models.CharField(choices=[Constants.choice1_name, Constants.choice2_name],widget=widgets.RadioSelectHorizontal(),blank=False,initial='blank')

Will throw the following error (as opposed to a warning, which occurred with previous versions of Otree):

(otree.E111) NonModelFieldAttr: Player has attribute "d", which is not a model field, and will therefore not be saved to the database.

I imagine that the best way to resolve this would be to declare d in the for loop as an IntegerField object and then cast it as an int using the int() method as follows:

 num_decs = models.IntegerField(initial=8)
 d = models.IntegerField(default=0)
 for d in range(1, num_decs.__int__() + 1):

But I receive an error that IntegerField lacks a int() method. What should I do to cast an IntegerField as an int for a for loop? Thanks!

Krabo
  • 31
  • 6

1 Answers1

1

For those of you who are out and about using Otree, I have a solution!

Add the following code to the end of your for loop:

del d
del num_decs

And you shouldn't have any problems :)

Krabo
  • 31
  • 6