Custom SQL problem

50 views
Skip to first unread message

elemental

unread,
Dec 10, 2006, 6:02:50 AM12/10/06
to Django users
I need to run a custom sql query in order to sum the results of a
column. I've followed the method outlined here:

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?

Don Arbow

unread,
Dec 10, 2006, 2:26:20 PM12/10/06
to django...@googlegroups.com
On Dec 10, 2006, at 3:02 AM, elemental wrote:

I need to run a custom sql query in order to sum the results of a
column. I've followed the method outlined here:


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


Look at the error message: 'function' object has no attribute 'execute'. You've set the cursor variable to connection.cursor, which without the parentheses, is a function object, not a function call. Change the line to

cursor = connection.cursor()

and that should fix the error.

Don


Reply all
Reply to author
Forward
0 new messages