object has no attribute 'count'

1,152 views
Skip to first unread message

wubble u

unread,
Mar 15, 2010, 5:44:35 PM3/15/10
to Django users
Hi All,

I'm trying to get a count of the number of rows in my queryset,
basically a SELECT COUNT(*).

According to the django documentation, you can simply use
queryset.count(), but I'm getting the error:

object has no attribute 'count'

my view code is:

hResults = Results.objects.select_related().get(home_team_id = '3')
hResCount = hResults.count()

my model is:

class Results(models.Model):
results_id = models.AutoField(primary_key=True)
league_id = models.ForeignKey(Leagues)
home_team_id = models.ForeignKey(Teams, related_name='home')
away_team_id = models.ForeignKey(Teams, related_name='away')

I've even tried len(hResults) and I get the same error.

The count attribute seems to work ok though if I just do
Results.objects.all()

Does anyone have any ideas why this errors?

Regards,

wubble u

Shawn Milochik

unread,
Mar 15, 2010, 5:46:17 PM3/15/10
to django...@googlegroups.com
If you're doing a 'get,' then you're always going to return exactly one results (or get an error).

If you do a 'filter' you can use count().

Shawn


Masklinn

unread,
Mar 15, 2010, 6:03:10 PM3/15/10
to django...@googlegroups.com

If you go read the documentation for the get() method (http://docs.djangoproject.com/en/dev/ref/models/querysets/#id5), you'll soon realize it can only ever return a single object. No object and multiple objects both result in an exception being thrown. Your result is therefore a Results instance, not a queryset. Results model having no attribute `count` and not being a collection, I find sensible that calling .count() or len() on it doesn't work and believe you're misunderstanding what you're doing.

wubble u

unread,
Mar 16, 2010, 5:57:49 PM3/16/10
to Django users
That's great, sorted it. Thanks for your help
Reply all
Reply to author
Forward
0 new messages