On Feb 3, 8:31 pm, "R. K." <[EMAIL PROTECTED]> wrote: > Hello, I'm wondering if there is such thing in django? For example, > forum has topics, and topics has posts, so it would be post has > foreign key to topic, and topic has foreign key to forum. And the > question is, how to get how many posts forum has? > Thanks in advance, > R. K.
The documentation on related objects is here: http://www.djangoproject.com/documentation/db-api/#related-objects The topics in each Forum will be available to that forum as foruminstance.topic_set.all(), and the posts on each Topic available as topicinstance.post_set.all(). That's provided your models are called Forum, Topic, and Post, of course. If you do it this way, you first have to get the topic_set for a given forum, and then iterate over each topic in that set, getting all its posts. Or you can go the other way, starting with the Post object, and filtering for a particular forum. The docs for this one are here: http://www.djangoproject.com/documentation/db-api/#lookups-that-span-relationships I don't have an equivalent setup to test on, but I believe the filter you want looks like this: posts = Post.objects.filter(topic__forum__name="myforumname") # double underscores in both spots provided your forums have a 'name' attribute and that's what you're selecting for. That ought to work; someone will probably correct me in a moment... Yours, Eric --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---

