OK so I have solved this, the flaw being my understanding of the
cacheing of Django database QuerySets.  I still have not eliminated
the double auth_user request because one is made in the
@login_required directive and the other is made in the view code to
pass user data to the template.  However it is easy to see how in
Django:
      # Set the course_id
      course_id = enrollment.course.id

      # Get the requested course
      course = Course.objects.get(id__exact=course_id)

Will result in two separate queries to get the course with that id.
It can be solved easily with:
      # Get requested course and set the course_id
      course = enrollment.course.id
      course_id = course.id

which reduces the number of SQL queries by 1 each page request or
shaves off about .4ms per page view.

-Matteius

On Nov 1, 1:24 pm, Matteius <matte...@gmail.com> wrote:
> In this case there is no form POST data so that code gets skipped.  I
> started profiling with the django_debugger_toolbar and my SQL data
> results are at the bottom.  There are duplicate SQL select statements
> for handin_course and auth_user which doesn't make sense to me as I
> understand how views should work ...
>
> #################################################
> # courseassignments - Course Assignments View
> #################################################
> @login_required
> def course_assignments(request, enrollment_id):
>    # Request enrollment
>    enrollment = Enrollment.objects.get(id__exact=enrollment_id)
>
>    # Determine access level and create default behavior
>    flag = verifyEnrollment(request, enrollment)
>    template = loader.get_template('handin/course_blocked.html')
>    context = RequestContext(request, {"user": request.user, "flag":
> flag} )
>
>    # If access is granted
>    if flag == 1:
>       # If we have post data process the form
>       if request.POST:
>
>          # Determine if submission is late
>          assignment =
> Assignment.objects.get(id__exact=request.POST['assignment'])
>          onTime = True
>          try:
>             # First see if there is a due date override
>             dueDate =
> DueDateOverride.objects.filter(enrollment=enrollment.id).filter(assignment=­assignment.id)
> [0]
>             if
> isLateNow( adjustDateDays( adjustDateWeeks(enrollment.start_date,
> dueDate.weeks_after), dueDate.days_after)):
>                onTime = False
>          except IndexError:
>             # If not, see if there is a global due date
>             if assignment.apply_due_date:
>                if
> isLateNow( adjustDateDays( adjustDateWeeks(enrollment.start_date,
> assignment.weeks_after), assignment.days_after)):
>                   onTime = False
>
>          instance = Submission(enrollment_id=enrollment.id,
> assignment_id=request.POST['assignment'], on_time=onTime)
>          newSubmission = SubmissionForm(request.POST, request.FILES,
> instance=instance)
>          newSubmission.save()
>          return
> HttpResponseRedirect(reverse('classcomm.handin.views.course_assignments',
> args=(enrollment.id,)))
>
>       # Set the course_id
>       course_id = enrollment.course.id
>
>       # Get the requested course
>       course = Course.objects.get(id__exact=course_id)
>
>       # Find the assignments for the requested course
>       assignments = Assignment.objects.all().filter(course=course_id)
>
>       # Find the grades for the current enrollment
>       grades = Grade.objects.all().filter(enrollment=enrollment.id)
>
>       # Find the submissions for the current enrollment
>       submissions =
> Submission.objects.all().filter(enrollment=enrollment.id)
>
>       # Find the due date overrides for the current enrollment
>       dueDateOverrides =
> DueDateOverride.objects.all().filter(enrollment=enrollment.id)
>
>       # Create assignmentDataList for easy template cycle
>       # [Assignment, current Submission, current Grade, current
> DueDateOverride]
>       assignmentDataList = list()
>       for assignment in assignments:
>          # Find the submission
>          currentSubmission = None
>          for submission in submissions:
>             if submission.assignment_id == assignment.id:
>                currentSubmission = submission
>          # Find the grade
>          currentGrade = None
>          for grade in grades:
>             if grade.assignment_id == assignment.id:
>                currentGrade = grade
>          # Find a due date override
>          currentDDO = None
>          for dueDateOverride in dueDateOverrides:
>             if dueDateOverride.assignment_id == assignment.id:
>                currentDDO = dueDateOverrides
>          # Add the tupple to a list
>          assignmentDataList.append( (assignment, currentSubmission,
> currentGrade, currentDDO) )
>
>       # Create an assignment submission form
>       form = SubmissionForm()
>
>       # Set template; Create context
>       template = loader.get_template('handin/course_assignments.html')
>       context = RequestContext(request, {"user": request.user,
> "course": course, "form": form,
>          "enrollment": enrollment, "assignmentDataList":
> assignmentDataList} )
>
>    # Return page view
>    return HttpResponse( template.render(context) )
> # End Def
>
> SQL Queries
> Time (ms)       Action  Stacktrace      Query
> 0.61    SELECT
> EXPLAIN
>
> Toggle Stacktrace
>
> SELECT `django_session`.`session_key`,
> `django_session`.`session_data`, `django_session`.`expire_date` FROM
> `django_session` WHERE (`django_session`.`session_key` =
> 97bf63456064cb8d5682f762a4d7faef AND `django_session`.`expire_date` >
> 2010-11-01 11:55:14 )
> Line    Method  File
> 132     _worker         build/bdist.linux-i686/egg/flup/server/threadpool.py
> 669     run     build/bdist.linux-i686/egg/flup/server/fcgi_base.py
> 705     process_input   build/bdist.linux-i686/egg/flup/server/
> fcgi_base.py
> 805     _do_params      build/bdist.linux-i686/egg/flup/server/fcgi_base.py
> 789     _start_request  build/bdist.linux-i686/egg/flup/server/
> fcgi_base.py
> 574     run     build/bdist.linux-i686/egg/flup/server/fcgi_base.py
> 1159    handler         build/bdist.linux-i686/egg/flup/server/fcgi_base.py
> 0.46    SELECT
> EXPLAIN
>
> Toggle Stacktrace
>
> SELECT `auth_user`.`id`, `auth_user`.`username`,
> `auth_user`.`first_name`, `auth_user`.`last_name`,
> `auth_user`.`email`, `auth_user`.`password`, `auth_user`.`is_staff`,
> `auth_user`.`is_active`, `auth_user`.`is_superuser`,
> `auth_user`.`last_login`, `auth_user`.`date_joined` FROM `auth_user`
> WHERE `auth_user`.`id` = 1
> Line    Method  File
> 132     _worker         build/bdist.linux-i686/egg/flup/server/threadpool.py
> 669     run     build/bdist.linux-i686/egg/flup/server/fcgi_base.py
> 705     process_input   build/bdist.linux-i686/egg/flup/server/
> fcgi_base.py
> 805     _do_params      build/bdist.linux-i686/egg/flup/server/fcgi_base.py
> 789     _start_request  build/bdist.linux-i686/egg/flup/server/
> fcgi_base.py
> 574     run     build/bdist.linux-i686/egg/flup/server/fcgi_base.py
> 1159    handler         build/bdist.linux-i686/egg/flup/server/fcgi_base.py
> 0.30    SELECT
> EXPLAIN
>
> Toggle Stacktrace
>
> SELECT `handin_enrollment`.`id`, `handin_enrollment`.`student_id`,
> `handin_enrollment`.`course_id`, `handin_enrollment`.`start_date`,
> `handin_enrollment`.`enrollment_length`,
> `handin_enrollment`.`access_mode`, `handin_enrollment`.`mentor_id`
> FROM `handin_enrollment` WHERE `handin_enrollment`.`id` = 3
> Line    Method  File
> 132     _worker         build/bdist.linux-i686/egg/flup/server/threadpool.py
> 669     run     build/bdist.linux-i686/egg/flup/server/fcgi_base.py
> 705     process_input   build/bdist.linux-i686/egg/flup/server/
> fcgi_base.py
> 805     _do_params      build/bdist.linux-i686/egg/flup/server/fcgi_base.py
> 789     _start_request  build/bdist.linux-i686/egg/flup/server/
> fcgi_base.py
> 574     run     build/bdist.linux-i686/egg/flup/server/fcgi_base.py
> 1159    handler         build/bdist.linux-i686/egg/flup/server/fcgi_base.py
> 181     course_assignments      /home2/geekshac/django_projects/classcomm/
> handin/views.py
> 0.29    SELECT
> EXPLAIN
>
> Toggle Stacktrace
>
> SELECT `auth_user`.`id`, `auth_user`.`username`,
> `auth_user`.`first_name`, `auth_user`.`last_name`,
> `auth_user`.`email`, `auth_user`.`password`, `auth_user`.`is_staff`,
> `auth_user`.`is_active`, `auth_user`.`is_superuser`,
> `auth_user`.`last_login`, `auth_user`.`date_joined` FROM `auth_user`
> WHERE `auth_user`.`id` = 1
> Line    Method  File
> 132     _worker         build/bdist.linux-i686/egg/flup/server/threadpool.py
> 669     run     build/bdist.linux-i686/egg/flup/server/fcgi_base.py
> 705     process_input   build/bdist.linux-i686/egg/flup/server/
> fcgi_base.py
> 805     _do_params      build/bdist.linux-i686/egg/flup/server/fcgi_base.py
> 789     _start_request  build/bdist.linux-i686/egg/flup/server/
> fcgi_base.py
> 574     run     build/bdist.linux-i686/egg/flup/server/fcgi_base.py
> 1159    handler         build/bdist.linux-i686/egg/flup/server/fcgi_base.py
> 184     course_assignments      /home2/geekshac/django_projects/classcomm/
> handin/views.py
> 395     verifyEnrollment        /home2/geekshac/django_projects/classcomm/
> handin/views.py
> 0.34    SELECT
> EXPLAIN
>
> Toggle Stacktrace
>
> SELECT `handin_course`.`id`, `handin_course`.`department_id`,
> `handin_course`.`name`, `handin_course`.`director_id`,
> `handin_course`.`open_enrollments`,
> `handin_course`.`enrollment_length`, `handin_course`.`description`
> FROM `handin_course` WHERE `handin_course`.`id` = 2
> Line    Method  File
> 132     _worker         build/bdist.linux-i686/egg/flup/server/threadpool.py
> 669     run     build/bdist.linux-i686/egg/flup/server/fcgi_base.py
> 705     process_input   build/bdist.linux-i686/egg/flup/server/
> fcgi_base.py
> 805     _do_params      build/bdist.linux-i686/egg/flup/server/fcgi_base.py
> 789     _start_request  build/bdist.linux-i686/egg/flup/server/
> fcgi_base.py
> 574     run     build/bdist.linux-i686/egg/flup/server/fcgi_base.py
> 1159    handler         build/bdist.linux-i686/egg/flup/server/fcgi_base.py
> 184     course_assignments      /home2/geekshac/django_projects/classcomm/
> handin/views.py
> 405     verifyEnrollment        /home2/geekshac/django_projects/classcomm/
> handin/views.py
> 0.24    SELECT
> EXPLAIN
>
> Toggle Stacktrace
>
> SELECT `handin_course`.`id`, `handin_course`.`department_id`,
> `handin_course`.`name`, `handin_course`.`director_id`,
> `handin_course`.`open_enrollments`,
> `handin_course`.`enrollment_length`, `handin_course`.`description`
> FROM `handin_course` WHERE `handin_course`.`id` = 2
> Line    Method  File
> 132     _worker         build/bdist.linux-i686/egg/flup/server/threadpool.py
> 669     run     build/bdist.linux-i686/egg/flup/server/fcgi_base.py
> 705     process_input   build/bdist.linux-i686/egg/flup/server/
> fcgi_base.py
> 805     _do_params      build/bdist.linux-i686/egg/flup/server/fcgi_base.py
> 789     _start_request  build/bdist.linux-i686/egg/flup/server/
> fcgi_base.py
> 574     run     build/bdist.linux-i686/egg/flup/server/fcgi_base.py
> 1159    handler         build/bdist.linux-i686/egg/flup/server/fcgi_base.py
> 216     course_assignments      /home2/geekshac/django_projects/classcomm/
> handin/views.py
> 19.62   SELECT
> EXPLAIN
>
> Toggle Stacktrace
>
> SELECT `handin_assignment`.`id`, `handin_assignment`.`course_id`,
> `handin_assignment`.`name`, `handin_assignment`.`details`,
> `handin_assignment`.`points_possible`,
> `handin_assignment`.`display_points_possible`,
> `handin_assignment`.`provided_files`,
> `handin_assignment`.`apply_due_date`,
> `handin_assignment`.`weeks_after`, `handin_assignment`.`days_after`,
> `handin_assignment`.`allow_late`,
> `handin_assignment`.`disable_submissions` FROM `handin_assignment`
> WHERE `handin_assignment`.`course_id` = 2
> Line    Method  File
> 132     _worker         build/bdist.linux-i686/egg/flup/server/threadpool.py
> 669     run     build/bdist.linux-i686/egg/flup/server/fcgi_base.py
> 705     process_input   build/bdist.linux-i686/egg/flup/server/
> fcgi_base.py
> 805     _do_params      build/bdist.linux-i686/egg/flup/server/fcgi_base.py
> 789     _start_request  build/bdist.linux-i686/egg/flup/server/
> fcgi_base.py
> 574     run     build/bdist.linux-i686/egg/flup/server/fcgi_base.py
> 1159    handler         build/bdist.linux-i686/egg/flup/server/fcgi_base.py
> 233     course_assignments      /home2/geekshac/django_projects/classcomm/
> handin/views.py
> 8.19    SELECT
> EXPLAIN
>
> Toggle Stacktrace
>
> SELECT `handin_submission`.`id`, `handin_submission`.`enrollment_id`,
> `handin_submission`.`assignment_id`, `handin_submission`.`date`,
> `handin_submission`.`file`, `handin_submission`.`on_time`,
> `handin_submission`.`add_checkscript` FROM `handin_submission` WHERE
> `handin_submission`.`enrollment_id` = 3
> Line    Method  File
> 132     _worker         build/bdist.linux-i686/egg/flup/server/threadpool.py
> 669     run     build/bdist.linux-i686/egg/flup/server/fcgi_base.py
> 705     process_input   build/bdist.linux-i686/egg/flup/server/
> fcgi_base.py
> 805     _do_params      build/bdist.linux-i686/egg/flup/server/fcgi_base.py
> 789     _start_request  build/bdist.linux-i686/egg/flup/server/
> fcgi_base.py
> 574     run     build/bdist.linux-i686/egg/flup/server/fcgi_base.py
> 1159    handler         build/bdist.linux-i686/egg/flup/server/fcgi_base.py
> 236     course_assignments      /home2/geekshac/django_projects/classcomm/
> handin/views.py
> 9.72    SELECT
> EXPLAIN
>
> Toggle Stacktrace
>
> SELECT `handin_grade`.`id`, `handin_grade`.`enrollment_id`,
> `handin_grade`.`assignment_id`, `handin_grade`.`points_earned`,
> `handin_grade`.`returned_files`, `handin_grade`.`comments` FROM
> `handin_grade` WHERE `handin_grade`.`enrollment_id` = 3
> Line    Method  File
> 132     _worker         build/bdist.linux-i686/egg/flup/server/threadpool.py
> 669     run     build/bdist.linux-i686/egg/flup/server/fcgi_base.py
> 705     process_input   build/bdist.linux-i686/egg/flup/server/
> fcgi_base.py
> 805     _do_params      build/bdist.linux-i686/egg/flup/server/fcgi_base.py
> 789     _start_request  build/bdist.linux-i686/egg/flup/server/
> fcgi_base.py
> 574     run     build/bdist.linux-i686/egg/flup/server/fcgi_base.py
> 1159    handler         build/bdist.linux-i686/egg/flup/server/fcgi_base.py
> 241     course_assignments      /home2/geekshac/django_projects/classcomm/
> handin/views.py
> 17.95   SELECT
> EXPLAIN
>
> Toggle Stacktrace
>
> SELECT `handin_duedateoverride`.`id`,
> `handin_duedateoverride`.`enrollment_id`,
> `handin_duedateoverride`.`assignment_id`,
> `handin_duedateoverride`.`weeks_after`,
> `handin_duedateoverride`.`days_after`,
> `handin_duedateoverride`.`allow_late` FROM `handin_duedateoverride`
> WHERE `handin_duedateoverride`.`enrollment_id` = 3

-- 
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.

Reply via email to