I have a model Iris with data about flowers (commonly used in R). I'm trying to get the xdata attribute and the ydata attribute for pieChart but it keeps failing. My code looks like the following:
The model
class Iris(models.Model):
species = models.CharField(max_length=100)
sepal_length = models.DecimalField(max_digits=10,decimal_places=2,null=True,blank=True)
sepal_width = models.DecimalField(max_digits=10,decimal_places=2,null=True,blank=True)
petal_length = models.DecimalField(max_digits=10,decimal_places=2,null=True,blank=True)
petal_width = models.DecimalField(max_digits=10,decimal_places=2,null=True,blank=True)
def as_json(self):
return dict(
species = self.species,
sepal_width =self.sepal_width,
sepal_length= self.sepal_length,
petal_width=self.petal_width,
petal_length=self.petal_length)
The view
def index(request):
irisData = [iris.as_json() for iris in Iris.objects.all()]
irisJson = simplejson.dumps(irisData)
My problem is that, every time I try to initialize the pieChart xdata attribute, I get the error:
Django Version: 1.6.5
Exception Type: TypeError
Exception Value: string indices must be integers, not str
The troublesome line is
xdata=irisJson['species']
How exactly should I supply the xdata values from Iris json array using attribute name?
I have a model Iris with data about flowers (commonly used in R). I'm trying to get the xdata attribute and the ydata attribute for pieChart but it keeps failing. My code looks like the following: The model
The view
My problem is that, every time I try to initialize the pieChart xdata attribute, I get the error:
The troublesome line is
How exactly should I supply the xdata values from Iris json array using attribute name?
The irisJson json data looks like this:
[{"sepal_width": 3.50, "petal_width": 0.20, "species": "setosa", "sepal_length": 5.10, "petal_length": 1.40}, {"sepal_width": 3.00, "petal_width": 0.20, "species": "setosa", "sepal_length": 4.90, "petal_length": 1.40}, {"sepal_width": 3.20, "petal_width": 0.20, "species": "setosa", "sepal_length": 4.70, "petal_length": 1.30}, {"sepal_width": 3.10, "petal_width": 0.20, "species": "setosa", "sepal_length": 4.60, "petal_length": 1.50}, {"sepal_width": 3.60, "petal_width": 0.20, "species": "setosa", "sepal_length": 5.00, "petal_length": 1.40}, {"sepal_width": 3.90, "petal_width": 0.40, "species": "setosa", "sepal_length": 5.40, "petal_length": 1.70}]