python - django - how to get searched object in template thru relatedmanager -
i trying write search logic. stuck here.
i have location model , rate model. each location can have multiple rates. these classes
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)
now if user search location rate 3
, in view
def search(request): rate = request.get.get('get') #all rates allrates = rate.objects.filter(rate=rate) #all locations rate locations = location.objects.filter(rate__in=allrates) return render_to_response('result.html',{'locations':locations},context_instance=requestcontext(request))
and in template:
{% loc in locations %} {{loc.rate.rate}} <--------- wrong! how searched rate 3 here?? {% endfor %}
but since every location object can have multiple rates, {{loc.rate.rate}}
doesnot work. want is, wanted rate - here 3 searched for.
can give me hint or please.
many thanks
this should work -
{% loc in locations %} {% rate in loc.rate %} {{ rate.rate }} {% endfor %} {% endfor %}
Comments
Post a Comment