oTree Forum >

ExtraModel - can records be updated?

#1 by ErikOSorensen

I coded a bunch of experiments in Django (without oTree) a while back -- now I am attempting oTree(-lite) for the first time. 

Reading a .csv file with information to present to each participant: One record to be assigned to each participant. I am attempting to use an ExtraModel (this is the simpler case):

class WorkerSituation(ExtraModel):
    pair_id = models.StringField()
    treatment = models.StringField()
    pie_size = models.IntegerField()
    assigned = models.StringField(default="no")
    timestamp = models.FloatField(default=None, null=True)

def creating_session(session: Session):
    rows = read_csv('consent_worker/worker_situations.csv', WorkerSituation)
    random.shuffle(rows)
    for row in rows:
        WorkerSituation.create(
            pair_id = row['pair_id'],
            treatment = row['treatment'],
            pie_size = row['pie_size']
        )

I would like incoming participants to be assigned a row in the WorkerSituation table, updating the assigned and timestamp variable to indicate that this record was put into use at some time, and then later a) Updating the "assigned" to confirmed (when participants finish), or resetting them as available if the participant times out. 

Am I right that there are no update methods on the ExtraModel ? Should I instead aim to use a separate database and potentially an ORM like SQLAlchemy on top of that? That would also give me transactions on treatment assignment without thread.locking, which would be nice -- but at the cost of running separate databases for oTree objects and the treatment assignments. 

I would apprectiate any advice and pointers to more idiomatic solutions to this problem. My restriction is that I need to have 1:1 mapping between participants and a pre-defined list of assignments.

Best regards,
Erik

#2 by ccrabbe

Hi Erik -

To answer your specific question, The oTree docs demonstrate recalling (from the db) the ExtraModels that you have created using the filter() method:

https://otree.readthedocs.io/en/latest/misc/advanced.html?highlight=extramodel#extramodel

Once you have the WorkerSituation object you wish to update, you can assign the new values to its timestamp and assigned model fields, and then (if I recall) make sure to call save() to make sure that the changes are pushed back into the db.

From a more zoomed-out point of view, from what you've described, it may not be necessary to use ExtraModel.  I often read in a csv file into a list of dicts,  and just store the list in session.vars.  Then it is accessible from anywhere in oTree, and can just be accessed from memory instead of needing to make db calls (which I have been told is the most common cause of an oTree app running slowly).

Good luck,
--Chris

If you need the timestamp and assigned data to persist in the db after your session is over (to access it from a later session perhaps?) then the ExtraModel

Write a reply

Set forum username