models: refererence to kpi_template plus a value...


I’m a django learner… and I need some tip on how to set proper model relationship…

Lets suppose to have a kpi_template table defined as follow:

class kpi_template(models.Model):
name = models.CharField(max_length=250, verbose_name = “KPI label”)
descr = models.CharField(max_length=20, verbose_name = “KPI description”)
min = models.IntegerField(blank=True, null=True, verbose_name = “KPI min value”)
max = models.IntegerField(blank=True, null=True, verbose_name = “KPI max value”)

I would like now to be able to reference more of these elements along with a current value similar to this json reference:

element[kpi]= [ ‘kpi_001’: 123, ‘kpi_002’: 453 ]

where ‘kpi_xxx’ ideally should be the foreign_key to kpi_template while value the associated data…

How can I structure element to implement this ?
Should I make use of models.jsonfield ? If so… can I keep the foreignkey relation to easily retrieve all associated informations ?

Ideally querying for element should return all the associated kpi list with values…


I’m not following what you’re trying to ask here.

Are you saying that the “001” in ‘kpi_001’ would be the pk of a row in kpi_template?

What is the “kpi” referring to in element[kpi]?

Are you trying to generate this structure, or are you receiving data that looks like this and are trying to store it in your database?

A bit more elaboration and clarity of what exactly you’re trying to do here, along with some explanations of the expected flow of the data would be really helpful.


Hello Ken,

basically I need to allow a user to decide how many kpi (a kpi is like a sensor type / value pair) to keep track and associated these to multiple group of objects (ie: network, radio, power, …).

So I was thinking first to define a table to host all possible kpi template. Ie: ‘kpi-xxx’ will have its own structure defining name/description/… Same kpi definition can be reused/applied to different elements.

Now I’ve to figure out best way to add a current reading value…

A possibility (mention in former post) would be to define on the object-id an element hosting mulltiple refences to the kpi_template-id along with the reading (aka jsonfield tuple where a parameter is a fk)…

…but another possibility I’m just thinking now is to simply define a kpi table that refers the kpi-template-id + the object-id + the reading. Probably this is a nice way to just use single row of kpi data that can be fetched based on objects… I need to experiment a bit on this approah.

Thx, A.