我正在尝试在1个查询中获取外键的外键,但无法正常工作
模型
date = "2019-07-13 14:40:50 GMT"
let a = {
"status": "ok",
"data": {
"Gallery": {
"gallery_id": "***ID***",
"GalleryImage": [{
"image_id": "***ID***",
"gallery_id": "***ID***",
"Image": {
"image_id": "***ID***",
"gallery_id": "***ID***",
"ImageLink": {
"link": "URLA"
},
"Iptc": {
"sdate": "2019-07-11 14:40:50 GMT"
}
}
},
{
"image_id": "***ID***",
"gallery_id": "***ID***",
"Image": {
"image_id": "***ID***",
"gallery_id": "***ID***",
"ImageLink": {
"link": "***URLB***"
},
"Iptc": {
"sdate": "2019-07-12 14:40:50 GMT"
}
}
},
{
"image_id": "***ID***",
"gallery_id": "***ID***",
"Image": {
"image_id": "***ID***",
"gallery_id": "***ID***",
"ImageLink": {
"link": "***URLC***"
},
"Iptc": {
"sdate": "2019-07-13 14:40:50 GMT"
}
}
}
]
}
}
}
let arr = a.data.Gallery.GalleryImage;
for (i = 0; i < arr.length; i++) {
if (arr[i].Image && arr[i].Image.Iptc && arr[i].Image.Iptc.sdate && arr[i].Image.Iptc.sdate == date) {
console.log(arr[i].Image.ImageLink.link)
}
}
代码
当我拿到所有冰柜时,此方法有效
class Storage(models.Model):
name = models.CharField(max_length=200,
null=False,
blank=False,
unique=True)
class Freezer(models.Model):
name = models.CharField(max_length=200,
null=False,
blank=False,
unique=True)
storage = models.ForeignKey(Storage,
models.CASCADE,
blank=True,
null=True,
related_name='freezer')
class Rack(models.Model):
name = models.CharField(max_length=200,
null=False,
blank=False,
unique=True)
freezer = models.ForeignKey(Freezer,
models.CASCADE,
blank=True,
null=True,
related_name='rack')
但是我无法获得有关此查询的信息
display_text = ", ".join([
"<a href={}>{}</a>".format(
reverse(
'admin:{}_{}_change'.format("admin",
"freezer"),
args=(items.pk, )), items)
for items in obj.freezer.all()
])
if display_text:
return mark_safe(display_text)
有什么建议对不起我吗?
答案 0 :(得分:0)
通过这种计算,您应该从所需的类(即机架)开始。
... for items in Rack.objects.filter(freezer__storage=obj)
(注意,通过将related_names设置为单数的“ rack”和“ freezer”,您已经使事情变得更加混乱。如果您必须将它们全部设置,我不建议这样做,应该至少使用复数名称。)