我有一个django应用程序,必须显示候选人名单中“需要”的国家名称和城市名称。为了更好地解释这一点,我有下一张图片:
所以,过程是这样的:首先,有人在他们各自的国家和城市发布“需要”。城市和乡村是不同的模式。
然后,候选人可以提出解决这一需求的建议。
我想在一个列表中看到,我发送给需要的所有报价(仅1个报价1个需要),但在我的html模板中,我想显示需要的国家和城市的名称。
这是我的模特
class requiter (models.Model):
requiter_name = models.CharField(max_length=200, null=True)
class country (models.Model):
country_name = models.CharField(max_length=200, null=True)
class city (models.Model):
city_name = models.CharField(max_length=200, null=True)
class candidate (models.Model):
full_name=models.CharField(max_length=230, null=True)
class need(models.Model):
requiter = models.ForeignKey(requiter, on_delete=models.CASCADE, null=True)
title= models.CharField(max_length=300, null=True)
description=models.TextField(null=True)
country=models.ForeignKey(country, on_delete=models.CASCADE, null=True)
city=models.ForeignKey(city, on_delete=models.CASCADE, null=True)
class offer(models.Model):
need = models.ForeignKey(need, on_delete=models.CASCADE, null=True)
candidate = models.ForeignKey(candidate, on_delete=models.CASCADE, null=True)
这是我的观点
def candidateprofile(request):
candata = candidate.objects.get(id=request.session['account_id'])
#HERE IS WHERE I WANT TO TAKE ALL THE OFFERS THAT THE CANDIDATE MADE, AND THE NAME OF THE COUNTRY AND CITY IN THE NEEDS
myoffers = offer.objects.filter(candidate_id = request.session['account_id'] )
#??????
dic.update({'candata': candata, 'jobapplies': jobapplies})
return render(request, 'my_app/candidateProfile.html', dic)
在sql中,等效的是next:
SELECT o.need_id, n.title, r.recruiter_name, c.country_name, y.city_name FROM offer AS o
INNER JOIN need AS n ON o.need_id = n.id
INNER JOIN requiter AS r ON n.recruiter_id = r.id
INNER JOIN country AS c ON n.country_id = c.id
INNER JOIN city AS y ON n.city_id = y.id
WHERE o.candidate_id = sended_parameter
但我不知道在django怎么做。请帮帮我!
1条答案
按热度按时间uujelgoq1#
你不应该这样想。你应该考虑如何得到你想要的东西。在您的例子中,您需要引用候选对象的need对象。所以:
现在您可以迭代需求并获得
need.country.country_name
以及need.city.city_name
; 这将是低效的,因为它将导致对每个访问的查询,所以我们可以添加select_related
到查询: