簡體   English   中英

django - 如何通過相關管理器獲取模板中的搜索對象

[英]django - how to get searched object in template thru relatedmanager

我正在嘗試編寫搜索邏輯。 但我被困在這里

我有位置模型和費率模型。 每個位置可以有多種費率。 這些是課程

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 %}

但由於每個位置對象都可以有多個速率, {{loc.rate.rate}}不起作用。 我想要的是,獲得確切的通緝率 - 這里搜索的是3。

請有人給我一些暗示或幫助。

非常感謝

這應該工作 -

{% for loc in locations %}
  {% for rate in loc.rate %}
    {{ rate.rate }}
  {% endfor %}
{% endfor %}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM