python 如何在Otree中访问custom_export中的数据?

qhhrdooz  于 2023-02-15  发布在  Python
关注(0)|答案(1)|浏览(155)

我尝试使用ExtraModelcustom_exportlive_pages导出数据。但是,当我进入devserver并检查数据选项卡时,它不在任何地方。如果我下载excel(页面右下角),新变量不包括在数据中。
我在哪里可以找到自定义导出的数据?或者我定义的函数是错误的?任何帮助都非常感谢。

参见下文MWE

from otree.api import *
import random

doc = """
Your app description
"""

class C(BaseConstants):
    NAME_IN_URL = 'mwe_export'
    PLAYERS_PER_GROUP = None
    NUM_ROUNDS = 1
    NUM_EMPLOYERS = 3

class Subsession(BaseSubsession):
    pass

class Group(BaseGroup):
    pass

class Player(BasePlayer):
    pass

class Offer(ExtraModel):
    group = models.Link(Group)
    sender = models.Link(Player)
    wage = models.IntegerField()
    effort = models.IntegerField()
    job_id = models.IntegerField()
    information_type = models.StringField()

# FUNCTIONS
def to_dict(offer: Offer):
    return dict(sender=offer.sender.id_in_group,
                wage=offer.wage,
                effort=offer.effort,
                job_id=offer.job_id,
                information_type=offer.information_type)

# PAGES
class MyPage(Page):
    @staticmethod
    def js_vars(player: Player):
        return dict(my_id=player.id_in_group)

    @staticmethod
    def live_method(player: Player, data):
        print(data)
        group = player.group
        job_id = random.randint(1, 1000)
        wage = data['wage']
        effort = data['effort']
        information_type = data['information_type']

        if data['information_type'] == 'offer':
            offer = Offer.create(group=group,
                                 sender=player,
                                 job_id=job_id,
                                 wage=wage,
                                 effort=effort,
                                 information_type=information_type)
            print(offer)
            print(to_dict(offer))
            return {0: to_dict(offer)}

page_sequence = [MyPage]

def custom_export(players):
    yield ['session.code', 'participant_code', 'id_in_session']
    offers = Offer.filter()

    for offer in offers:
        player = offer.sender
        participant = player.participant
        yield [participant.code, participant.id_in_session, offer.job_id, offer.wage, offer.effort]
3phpmpom

3phpmpom1#

在管理页面顶部的菜单中也有一个“数据”项。你的应用程序的自定义导出应该在“每个应用程序”标题下可用。

相关问题