0

我正在尝试编写搜索逻辑。但我被困在这里。

我有位置模型和费率模型。每个位置可以有多个费率。这些是类

class Location(models.Model):
  name = models.TextField()
  price = models.CharField(max_length=10)

class Rate(models.Model):
  location = models.ForeignKey(Location,related_name="rate")
  rate = models.IntegerField(max_length=10)

现在,如果用户使用费率搜索位置3,我将在视图中执行此操作

def search(request):
   rate = request.GET.get('get')
   #all rates
   allrates = Rate.objects.filter(rate=rate)
   #all locations with this rate
   locations = Location.objects.filter(rate__in=allrates)
   return render_to_response('result.html',{'locations':locations},context_instance=RequestContext(request))

在我的模板中:

{% for loc in locations %}
  {{loc.rate.rate}} <--------- wrong! how to get that searched rate 3 here?? 
{% endfor %}

但由于每个位置对象都可以有多个 Rates,因此{{loc.rate.rate}}不起作用。我想要的是,得到那个确切想要的利率 - 这里是搜索的 3。

有人可以给我一些提示或帮助吗?

非常感谢

4

1 回答 1

2

这应该工作 -

{% for loc in locations %}
  {% for rate in loc.rate %}
    {{ rate.rate }}
  {% endfor %}
{% endfor %}
于 2013-05-09T16:50:52.247 回答