django提供shell交互环境让我们查看自己的网页后台
(venv) D:\learning_log>python manage.py shell
Python 3.7.0 (default, Jun 28 2018, 08:04:48) [MSC v.1912 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from learning_logs.models import Topic
>>> Topic.objects.all()
<QuerySet [<Topic: Topic object>]>
这里返回了 Topic 的所有实例也就是条目
这里需要注意返回值是一个列表 叫做queryset(查询集)
下面我们继续遍历查询
>>> for topic in topics:
... print(topic.id,topic)
t = Topic.objects.gets(id = 1)
t.text #显示条目
t.date_added #显示添加时间
t.entry_set.all() #使用外键获取元素 注意这里的写法表示entry外键关联t
退出shell
ctrl+z + 回车