I am trying to write a search logic. but i am stuck here.
I have Location Model and Rate Model. Each Location can have multiple rates. these are 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 the user search for location with rate 3
, i will do this in view
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))
and in my template:
{% for loc in locations %}
{{loc.rate.rate}} <--------- wrong! how to get that searched rate 3 here??
{% endfor %}
But since every location object can have multiple Rates, {{loc.rate.rate}}
doesnot work. what i want is, to get that exactly wanted rate - here 3 which was searched for.
can someone give me some hint or help please.
many thanks