[英]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.