Hey there people:
Let's say that I have these models
======
class Post(models.Model):
user = models.ForeignKey(User)
category = models.ForeignKey(Categories)
title = models.CharField(max_length=50)
post = models.TextField()
date = models.DateTimeField(auto_now_add=True)
def __unicode__(self):
return self.title
class Comment(models.Model):
post = models.ForeignKey(Post)
approved = models.CharField(max_length=1, default='0')
name = models.CharField(max_length=40)
email = models.EmailField()
site = models.CharField(max_length=100)
ip_address = models.IPAddressField(null=True)
comment = models.TextField()
date = models.DateTimeField(auto_now_add=True)
======
I can perfecty render each post in a single page with a simple for statement
inside a template. I can also access the username by using {{
x.post.user.username }} without any problem but, what if I'm looping over
all my posts and I'd like to get how many comments each post has?? In a
regular django python loop, I would use something like:
p = Post.objects.get('""some_condition'"")
""" some code """
p.comment_set.count()
As I know, with templates I can go all the way back with relations between
tables (as I did with Post->User with {{ x.post.user.username }}) but, what
if i wanted to go the other way as I can do it in python/django with
"_set"?? Something like {{ x.comment_set.count }} (being x a post iterator).
I can do it with python and I can get the results by passing a dictionary
with everything to loop over it, but I'm sure there has to be an easier way
to do it
Thanks a lot for your help
jhv
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---