Using map() on a _set in a template?

Posted by Stuart Grimshaw on Stack Overflow See other posts from Stack Overflow or by Stuart Grimshaw
Published on 2010-12-26T23:28:33Z Indexed on 2010/12/26 23:54 UTC
Read the original article Hit count: 173

Filed under:
|
|

I have two models like this:

class KPI(models.Model):
    """KPI model to hold the basic info on a Key Performance Indicator"""
    title = models.CharField(blank=False, max_length=100)
    description = models.TextField(blank=True)
    target = models.FloatField(blank=False, null=False)
    group = models.ForeignKey(KpiGroup)
    subGroup = models.ForeignKey(KpiSubGroup, null=True)
    unit = models.TextField(blank=True)
    owner = models.ForeignKey(User)
    bt_measure = models.BooleanField(default=False)

class KpiHistory(models.Model):
    """A historical log of previous KPI values."""
    kpi = models.ForeignKey(KPI)
    measure = models.FloatField(blank=False, null=False)
    kpi_date = models.DateField()

and I'm using RGraph to display the stats on internal wallboards, the handy thing is Python lists get output in a format that Javascript sees as an array, so by mapping all the values into a list like this:

def f(x): return float(x.measure)
stats = map(f, KpiHistory.objects.filter(kpi=1)

then in the template I can simply use

{{ stats }}

and the RGraph code sees it as an array which is exactly what I want.

[87.0, 87.5, 88.5, 90]

So my question is this, is there any way I can achieve the same effect using Django's _set functionality to keep the amount of data I'm passing into the template, up until now I've been passing in a single KPI object to be graphed but now I want to pass in a whole bunch so is there anything I can do with _set

{{ kpi.kpihistory_set }}

dumps the whole model out, but I just want the measure field.

I can't see any of the built in template methods that will let me pull out just the single field I want.

How have other people handled this situation?

© Stack Overflow or respective owner

Related posts about python

Related posts about django