tags:

views:

56

answers:

2

Straightforward question - apologies if it is a duplicate, but I can't find the answer if so.

I have a User model and a Submission model, like this:

class Submission(models.Model):
    uploaded_by = models.ForeignKey('User')
class User(models.Model):
    name = models.CharField(max_length=250 )

How can I show the number of Submissions made by each user in the template? I've tried {{ user.submission.count }}, like this:

for user in users:
    {{ user.name }} ({{ user.submission.count }} submissions)

but no luck...

+3  A: 

Try this

{{user.submission_set.all|length}}
czarchaic
Yeah, you can even do {{ user.submission_set.count }}. Be sure to read up on django relations. They are so important to understanding models well.http://docs.djangoproject.com/en/dev/topics/db/models/#be-careful-with-related-name
Casey Stark
Thanks - and thanks @Casey for the tip, I will.
AP257
Thanks - I've read up but I have another quick question. If the Submission class has a boolean field called 'problem_submission', is there a way to get the count of all the related submissions where the 'problem_submission' field isn't set to True?
AP257
Something like that would probably be best with a custom template tag (which are really easy to write)
czarchaic
+1  A: 

You forgot the "set". It should be {{ user.submission_set.count }}. You can always change the related name, but the default is <fk class name>_set. For more see the relations documentation.

rz