a report problem

13 views
Skip to first unread message

MikeKJ

unread,
Feb 25, 2014, 11:10:16 AM2/25/14
to django...@googlegroups.com
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

Anyone got any better way of doing this cos this sure isn't working too well
   
Cheers

Erik Cederstrand

unread,
Feb 25, 2014, 11:48:34 AM2/25/14
to Django Users
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

MikeKJ

unread,
Feb 25, 2014, 12:24:57 PM2/25/14
to django...@googlegroups.com
Cheers Erik
I'll look at aggregation as well
Dates model is because it is supposed to operate on a very limited set of dates

Reply all
Reply to author
Forward
0 new messages