Model Method

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Sample(models.Model):
name = models.CharField(max_length=30)
def testing(self):
if self.id == 2:
return "<b>%d</b>" % self.id
else:
return self.id
###########################################################
#view
@render_to('test.html')
def test(request):
lam = Sample.objects.order_by('?')
return {'lam':lam}
###########################################################
#template
{% for i in lam %}
{{ i.name }} {{ i.testing }}<br />
{% endfor %}