http://www.djangoproject.com/documentation/model_api/#custom-managers
As best I can tell, I've setup everything properly, however, I'm
receiving the following error:
'function' object has no attribute 'execute'
The code I am using, first starting with models.py:
class FooManager(models.Manager):
def total_time(self):
from django.db import connection
cursor = connection.cursor
cursor.execute("SELECT sum(time) AS total_time FROM table_name")
row = cursor.fetchone()
return row
class Foo(models.Model):
...model definitions
objects = FooManager()
class Admin:
pass
def __str__(self):
return self.comments
In views.py, I have the following:
def index(request):
recent_list = Foo.objects.all().order_by('-foo_number')[:15]
total_count = Foo.objects.count()
total_time = Foo.objects.total_time()
t = loader.get_template('foo/index.html')
c = Context({
'recent_list': recent_list,
'total_count' : total_count,
'total_time' : total_time,
})
return HttpResponse(t.render(c))
Can anybody spot what the problem is?
I need to run a custom sql query in order to sum the results of acolumn. I've followed the method outlined here:As best I can tell, I've setup everything properly, however, I'mreceiving the following error:'function' object has no attribute 'execute'The code I am using, first starting with models.py:class FooManager(models.Manager):def total_time(self):from django.db import connectioncursor = connection.cursorcursor.execute("SELECT sum(time) AS total_time FROM table_name")row = cursor.fetchone()return row