One of my models has attributes that are not stored in the database. Everything is fine at the view and model level but I cannot seem to display these 'non-database' attributes in my template.
Here's some example code, an artificial example that mirrors the actual problem domain, to demonstrate the undesired behavior.
The view:
def odometer(request):
cars = Car.objects.all()
for car in cars:
car.read_meters()
context = {'cars': cars}
return render_to_response('odometer.html', context)
The models:
class Car(models.Model):
name = models.CharField(_('name'), max_length=100, unique=True)
def read_meters(self):
for meter in self.meter_set.all():
meter.read()
def __unicode__(self):
return '%s' % self.name
class Meter(models.Model):
name = models.CharField(_('name'), max_length=100)
car = models.ForeignKey(Car)
difference = 0
previous = 0
changed = False
def read(self):
# this is completely artificial. in the real application we would interface with the hardware
# meter to get data
try:
previous_count = MeterReading.objects.filter(meter__id=self.id).order_by('-stamp')[0].count
except:
previous_count = 0
self.previous = previous_count
current_count = previous_count
if (random.randrange(0, 2) == 0):
self.difference = int(random.random() * 100)
if self.name == 'Odometer' or (random.randrange(0, 2) == 0):
current_count += self.difference
else:
current_count -= self.difference
if current_count < 0:
current_count = 0
if current_count > previous_count:
self.changed = True
new_reading = MeterReading()
new_reading.count = current_count
new_reading.meter = self
new_reading.save()
def __unicode__(self):
return '%s' % self.name
class MeterReading(models.Model):
count = models.IntegerField(_('count'))
stamp = models.DateTimeField(editable=False, auto_now_add=True)
meter = models.ForeignKey(Meter)
def __unicode__(self):
return '%s' % self.count
And the template:
{% for car in cars %}
<h2>{{ car }}</h2>
{% for meter in car.meter_set.all %}
<h3>{{ meter }}</h3>
<p>Difference: {{ meter.difference }}</p>
<p>Changed: {{ meter.changed }}</p>
<ul>
{% for reading in meter.meterreading_set.all %}
<li>{{ reading }}</li>
{% endfor %}
</ul>
{% endfor %}
{% endfor %}
The problem is 'meter.difference' and 'meter.changed' don't output the correct updated values. What am I doing wrong? Any advice is appreciated.
Thanks.
UPDATE: Updated code based on Daniel's answer:
The Car model:
class Car(models.Model):
name = models.CharField(_('name'), max_length=100, unique=True)
def read_meters(self):
for meter in self.meters:
meter.read()
def __unicode__(self):
return '%s' % self.name
@property
def meters(self):
if not hasattr(self, '_meters'):
self._meters = self.meter_set.all()
return self._meters
And the template:
{% for car in cars %}
<h2>{{ car }}</h2>
{% for meter in car.meters %}
<h3>{{ meter }}</h3>
<p>{{ meter.name }} difference: {{ meter.difference }}</p>
<p>Changed: {{ meter.changed }}</p>
<ul>
{% for reading in meter.meterreading_set.all %}
<li>{{ reading }}</li>
{% endfor %}
</ul>
{% endfor %}
{% endfor %}