Jes*_*sse 11 python django foreign-key-relationship
我有一个像这样的django模型:
class Profile_Tag(models.Model):
profile = models.ForeignKey(Profile)
tag = models.ForeignKey(Tag)
Run Code Online (Sandbox Code Playgroud)
和这样的观点:
pts = Profile_Tag.objects.all()
for pt in pts:
print pt.profile.id
Run Code Online (Sandbox Code Playgroud)
有没有办法访问配置文件foreignKey而不是每次都访问数据库?我不想查询配置文件表.我只想从Profile_Tag表中获取ID.
Dmi*_*nko 15
你可以这样做:
pt_ids = Profile_Tag.objects.values_list('profile', flat=True)
Run Code Online (Sandbox Code Playgroud)
这将返回您的ID列表.对于模型实例,还有另一种方法:
pts = Profile_Tag.objects.all()
for pt in pts:
print pt.profile_id
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2578 次 |
| 最近记录: |