简体   繁体   English

Djando与Pandas访问ForeignKey

[英]Django with Pandas accessing ForeignKey

I do not know Pandas so this may be trivial. 我不知道熊猫所以这可能是微不足道的。

I have this line of code: 我有这行代码:

admin_data = pd.DataFrame.from_records(administrations.values()).rename(columns = {'id':'administration_id', 'study_id': 'study_name', 'url_hash': 'link'})

which is getting data from the administration model ( administrations is a recordset) and using it. administration模型获取数据( administrations是记录集)并使用它。 Rather than using study_id I would like to get the study name. 而不是使用study_id我想获得研究名称。 With Djano ORM I would use study__name but I cannot do that here. 使用Djano ORM,我会使用study__name但我不能在这里做。

How can I get the study.name instead of study.id into admin_data ? 如何将study.name而不是study.id导入admin_data

models 楷模

class administration(models.Model):
    study = models.ForeignKey("study") # Study name
    ...

class study(models.Model):
    name = models.CharField(max_length = 51) # Study name
    ...

Query the columns you need and use the __ syntax to get the name through the foreign key: 查询所需的列并使用__语法通过外键获取名称

admin_data = pd.DataFrame.from_records(
    administrations.values(
        "id",
        "study__name",  # <-- get the name through the foreign key
        "url_hash",
    )
).rename(
    columns={
        "id": "administration_id",
        "study__name": "study_name",
        "url_hash": "link",
    }
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM