This works in python IDLE
class A:
b = 0
def __init__(self, value):
self.b = value
obj1 = A(1)
obj2 = A(2)
obj3 = A(3)
k = [obj1, obj2, obj3]
result = sum(i.b for i in k)
print str(result)
But this doesn’t work for django/python
from django.db import models
class Flatbar(models.Model):
width = models.IntegerField()
height = models.IntegerField()
def Area(self):
return self.width * self.height
class Section(models.Model):
flatbars = models.ManyToManyField(Flatbar)
def Area(self):
return str(sum(f.Area for f in self.flatbars))
Why is this not working and how would I do this using a lambda function?
The attribute for a
ManyToManyFieldyields a manager, and you madeAreaa method.