如果我在具有to_field列定义的模型中创建ForeignKey定义,是否可以稍后获取该to_field值?
class author(models.Model):
name=models.CharField(max_length=255)
customId=models.IntegerField(default=0, null=True, unique=True)
class book(models.Model):
name=models.CharField(max_length=255)
author=models.ForeignKey(author,to_field='customId')
如上所示,作者的外键使用to_field
键,稍后在我的代码中,我必须处理书本对象,但想找出作者链接到的字段。
答案 0 :(得分:0)
在Django中,您的_id
字段将通过在其中添加to_field
来定义。因此,您可以直接访问该book.author_id == book.author.customId
。
{{1}}
答案 1 :(得分:0)
您可以使用get_field获取import pandas as pd
url="......html'
df=pd.read_html(url)
for i in range(0,10):
print(df[i])
值
to_field
答案 2 :(得分:0)
似乎to_field
不是必需的,只需使用author.customId
来引用值。