1
我創建我的經理車庫和汽車。 我試圖計算當前車庫中所有車輛的貨幣價值。中級模型Django ORM
class Car(models.Model):
name = models.CharField(max_length=50)
price = models.DecimalField()
class GarageCar(models.Model):
car = models.ForeignKey('Car')
how_much = models.IntegerField()
class Garage(models.Model):
name = models.CharField("Garage_Name", max_length=30)
cars = models.ManyToManyField('GarageCar', blank=True, null=True)
我嘗試這樣:
def price_of_cars(request):
garages = Garage.objects.filter(..) #
total_price_of_cars_in_this_garages = 0
for a in garages:
for p in garages.cars:
total_price_of_cars_in_this_garages += (p.price * how_much)
return render_to_response('garage.html',
{'total_price_of_cars': total_price_of_cars_in_this_garages})
但返回:語法錯誤,如果我刪除how_much
返回錯誤:'ManyRelatedManager' 對象不是可迭代