Den 25/02/2014 kl. 17.10 skrev MikeKJ <
mike....@paston.co.uk>:
> model:
> class Dates(models.Model):
> this_date = models.DateField()
>
> class Booking(models.Model)
> seats = models.IntegerField()
> date = models.ForeignKey(Dates)
>
> report view:
> this_date = "01-01-2000"
> seats = 0
> daily_arrivals = []
> all = Booking.objects.all()
> for a in all:
> while a.date != this_date:
> seats += seats
> daily_seats.append("%s - %s" % (a.date, a.seats)
> this_date = a.date
>
> What I am trying to get at is a total of seats by date as a list to send to a template
That’s some pretty bizarre code. Try:
from collections import defaultdict
seats_map = defaultdict(int)
for b in Booking.objects.all():
seats_map[b.date.this_date] += b.seats
Or look at the aggregation options in the Django ORM. BTW, why even bother with the Dates model? Just make Booking.date a DateField.
Erik