tags:

views:

25

answers:

1

I'm brand new to Django, so the answer to this is probably very simple. However, I can't figure it out.

Say I have two bare-bones Models.

class Blog(models.Model):
    title = models.CharField(max_length=160)
    text = models.TextField()

class Comment(models.Model):
    blog = models.ForeignKey(Blog)
    text = models.TextField()

In the Python/Django shell, if I have a Blog object in a variable (say blog = Blog.objects.get(id=3)), how do I select all its child comments?

This doesn't seem to work: blog.objects.all()

+3  A: 

to follow foreign keys 'backwards' you use

blog.comment_set.all()
second
I can't believe I passed that up on the Making Queries page in the documentation! Thank you!
Vortico
Did you read the basic tutorial on django.org?
leoluk
Yes, but I thought the _set function was used on ManyToMany relationships for grabbing the list of ForeignKey parents, instead of the reverse of that.
Vortico