這工作在Python IDLEPython的Django中的models.py
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)
但這並不適用於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))
這是爲什麼不工作,我會怎麼做這使用一個lambda函數?
「不工作」是什麼意思?你會得到什麼錯誤? –