在Django中选择带有ForeignKey的对象的子代? [英] Select Children of an Object With ForeignKey in Django?

查看:67
本文介绍了在Django中选择带有ForeignKey的对象的子代?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Django的全新,因此答案很简单。但是,我无法弄清楚。

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

说我有两个基本模型。

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

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

在Python / Django外壳中,如果我在变量中包含Blog对象(例如 blog = Blog.objects.get(id = 3)),如何选择其所有子注释?

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?

这似乎不起作用: blog.objects.all()

推荐答案

跟随您使用的外键向后

to follow foreign keys 'backwards' you use

blog.comment_set.all()

这篇关于在Django中选择带有ForeignKey的对象的子代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆