Hi Scott Thanks for you help, unfortunately on trying sales = Sale.objects.filter(date_ created__range=(init_date,ends_date)) .values(date_ created__month) .aggregate(total_sales=Sum('total_value'))
This error appeared, global name 'date_created__month' is not defined Do you have another approach? Regards, Rogério Carrasqueira --- e-mail: rogerio.carrasque...@gmail.com skype: rgcarrasqueira MSN: rcarrasque...@hotmail.com ICQ: 50525616 Tel.: (11) 7805-0074 2010/10/28 Scott Gould <zinck...@gmail.com> > Personally I hate writing raw SQL so I would probably try something > like this (untested): > > sales = Sale.objects.filter(date_created__range=(init_date,ends_date)) > .values(date_ created__month) > .aggregate(total_sales=Sum('total_value')) > > sales_by_month = [(x.year, x.month, [y for y in sales if y.year == > x.year and y.month == x.month]) > for x in set([z.year, z.month for z in sales])] > > Should give you a list of (year, month, subqueryset) tuples. Of > course, there might be performance issues with nested list > comprehensions like that depending on the size of the initial > queryset, but at least it's a one-line, SQL-free solution! > > On Oct 28, 9:31 am, Rogério Carrasqueira > <rogerio.carrasque...@gmail.com> wrote: > > Hello! > > > > I'm having an issue to make complex queries in django. My problem is, I > have > > a model where I have the sales and I need to make a report showing the > sales > > amount per month, by the way I made this query: > > > > init_date = datetime.date(datetime.now()-timedelta(days=365)) > > ends_date = datetime.date(datetime.now()) > > sales = > > > Sale.objects.filter(date_created__range=(init_date,ends_date)).values(date_ > created__month).aggregate(total_sales=Sum('total_value')) > > > > At the first line I get the today's date past one year > > after this I got the today date > > > > at sales I'm trying to between a range get the sales amount grouped by > > month, but unfortunatelly I was unhappy on this, because this error > > appeared: > > > > global name 'date_created__month' is not defined > > > > At date_created is the field where I store the information about when the > > sale was done., the __moth was a tentative to group by this by month. > > > > So, my question: how to do that thing without using a raw sql query and > not > > touching on database independence? > > > > Thanks so much! > > > > Rogério Carrasqueira > > > > --- > > e-mail: rogerio.carrasque...@gmail.com > > skype: rgcarrasqueira > > MSN: rcarrasque...@hotmail.com > > ICQ: 50525616 > > Tel.: (11) 7805-0074 > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-us...@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com<django-users%2bunsubscr...@googlegroups.com> > . > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.