マネージャーのガレージと車を作成します。現在ガレージにあるすべての車の金銭的価値を数えようとしています。
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」オブジェクトは反復可能ではありません