Django shell中的查询操作
查询集:
>>>Topic.objects.all()
<QuerySet [<Topic: Chess>, <Topic: Rock Climbing>, <Topic: 国际象棋>, <Topic: 攀岩>]>
>>> Topic.objects.order_by('data_added') #按属性排序的查询集
<QuerySet [<Topic: Chess>, <Topic: Rock Climbing>, <Topic: 国际象棋>, <Topic: 攀岩>]>
依次输出:
>>> for topic in Topic.objects.all():
... print(topic.id,topic)
1 Chess
2 Rock Climbing
3 国际象棋
4 攀岩
ID查询:
>>>t = Topic.objects.get(id=1)
>>> t
<Topic: Chess>
属性获取:
>>> t.text
'Chess'
外键查询:
>>> t.entry_set.all() #相关模型(Entry)的小写、下划线(_)、单词set 进行查询
<QuerySet [<Entry: Chess, also known as European chess or chess, is a...>, <Entry: Chess consists of black and white pieces.Whether i...>]>

浙公网安备 33010602011771号