python - django related manager - in template -


i trying show latest rating score of location. have 2 tables(django models)

class location(models.model):    locationname = models.charfield()    ...  class rating(models.model):    von_location = models.foreignkey(location,related_name="locations_rate")    rating = models.integerfield() 

now in db, 1 location(id=1) has 3 ratings(1,2,4).

i want show latest record in rating location. can in template using related manager somehow?

my vision is:

all_locs = location.objects.all() 

then in template:

{% location in all_locs %}    {{ location.locations_rate.latest }} {% endfor %} 

can relatedmanager kind of things?

my answer in other related question:

models.py

class location(models.model):     locationname = models.charfield(max_length=100)      def __unicode__(self):         return self.locationname      def latest(self):         return rating.objects.values('rating').filter(von_location=self).order_by('-id')[0]  class rating(models.model):    von_location = models.foreignkey(location,related_name="locations_rate")    rating = models.integerfield()     def __unicode__(self):         return "{0}".format(self.rating) 

views.py

all_locs = location.objects.all() 

template

{% location in all_locs %}    {{ location.locationname }} - {{ location.latest.rating }}<br/> {% endfor %} 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -