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
If you do a 'filter' you can use count().
Shawn
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.