On Tue, Mar 8, 2011 at 1:23 AM, sushanth Reddy <sushant...@gmail.com> wrote:
> Hi Tom,
>
> Can you pleaseĀ  add few lines of code example,
> to make code more efficient?sorry for troubling.
>

Currently you query the DB to generate a list of users, and then for
each user you query the DB once for each date in the date range to
find out their expenses. If you have 100 users, and a 60 day reporting
period, your view will do 6000 queries to display the page. For this
sort of situation, you should be using 2 queries.

users = Profiles.objects.filter(...)
rp_begin = ... # start of reporting period
rp_end = ... # end of reporting period
expenses_data = Dataset.objects.filter(user__in=users,
date__gt=rp_begin, date__lt=rp_end).order_by('user',
'date').values_list('user_id', 'date', 'expense')

This will give you a list of values. You can either iterate through
this building up the HTML in your view (bad), or you can then
pre-process it a little bit into a format that would work nicely in a
template, and output it that way.

Cheers

Tom

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@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.

Reply via email to