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?
ManyToManyField
的属性产生一个管理器,并且您将Area
用作方法。
return str(sum(f.Area() for f in self.flatbars.all()))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.