利用 python manage.py shell 进入shell环境
源码
In [1]: from App.models import *
In [2]: main_shops=MainShop.objects.all()
In [3]: main_shops
Out[3]: <**QuerySet** [<MainShop: MainShop object (1)>, <MainShop: MainShop object (2)>, <MainShop: MainShop ob
ject (3)>, <MainShop: MainShop object (4)>, <MainShop: MainShop object (5)>, <MainShop: MainShop object (6)
>, <MainShop: MainShop object (7)>, <MainShop: MainShop object (8)>, <MainShop: MainShop object (9)>, <Main
Shop: MainShop object (10)>, <MainShop: MainShop object (11)>]>
In [4]: main_shops.0 # 使用场合出错,这是django模板所拥有的特性,通过使用句点符可以获得QuerySet其中的一个值
File "<ipython-input-4-f7f6615d011b>", line 1
main_shops.0
^
SyntaxError: invalid syntax
In [5]: main_shops[0].img #获得第一个商品的图片
Out[5]: 'http://img01.bqstatic.com//upload/activity/2016121616565087.png@90Q.png'
In [8]: type(main_shops[0])
Out[8]: App.models.MainShop # MainShop 模型类
In [9]: main_shops[0] #某一个具体的元素为MainShop类型,没毛病
Out[9]: <MainShop: MainShop object (1)>
In [10]: type(main_shops[0:1]) # 用切片切出来的是QuerySet类型
Out[10]: django.db.models.query.QuerySet
In [11]: main_shops[0:1]
Out[11]: <QuerySet [<MainShop: MainShop object (1)>]>
In [14]: isinstance(main_shops,(list,set)) #QuerySet 既不是list,也不是set
Out[14]: False
Pay Attention
用切片切出来的结果不一定就是列表,还可以是其它的类型,要看原先的类型是啥