python - last record in django models database -
i trying this:
i have table of rating records of locations. in table, 1 location can have multiple rating scores. want restrict search last record of same locations. example:
one location id
=1 has 3 rating scores: 1, 2, 4. when user searchs rating score 2, location should not appear, because last record 4.
edit
there 2 tables(django models): location , rating.
can this:
all_locations = location.objects.all()
then in template. locations_rating
related_name foreignkey locationid
in rating table.
{% location in all_locations %} {{ location.locations_rating }} {% endfor %}
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
Post a Comment