Tuesday, February 25, 2014

Re: a report problem

Den 25/02/2014 kl. 17.10 skrev MikeKJ <mike.jones@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

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/085B9396-6900-4430-AFD5-38E34FEC7634%40cederstrand.dk.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment