我创建我的经理车库和汽车。我正在尝试计算目前在车库中的所有汽车的货币价值。
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' 对象不可迭代