Let us solve this by an example-
I have a model as-
class item(models.Model):
item_number = models.AutoField(primary_key=True)
item_name = models.CharField(max_length=200)
lifetime_rating = models.DecimalField(max_digits=3,decimal_places=1, default=0)
current_rating = models.DecimalField(max_digits=3,decimal_places=1, default=0)
lifefeedbacknum = models.IntegerField(default=0)
currentfeedbacknum = models.IntegerField(default=0)
def __unicode__(self):
return self.item_name
def retlifetime_rating(self):
return self.lifetime_rating
Note method-retlifetime_rating which returns the value of lifetimerating for an object instance.
Now we wish to display lifetime ratings for all products
in views.py-
def showrating(request):
itemslist= models.item.objects.all()
return render(request, 'showrating.html', 'sitems' : itemslist)
the showrating.html file contains the following code snippet-
{% for element in sitems %}
<tr>
<td>{{ element }}</td>
<td>{{ element.retlifetime_rating }}</td>
</tr>
{% endfor %}
basically if you wish to display different fields of an object, you need to have a corresponding method call to return that field
off-course there are other ways to do it but this is most likely the simplest and easiest to implement