django-orm查询操作
In [19]: from bookstore.models import Book
In [20]: a1=Book.objects.all()
In [21]: a1
Out[21]: <QuerySet [<Book: Book object (1)>, <Book: Book object (2)>, <Book: Book object (3)>, <Book: Book object (4)>, <Book: Book object (5)>]>
In [22]: #类数组
In [23]: for book in a1:
...: print(book.title)
...:
python
Django
JQury
Linux
HTML5
进一步显示book对象
In [1]: from bookstore.models import Book
In [2]: b1=Book.objects.all()
In [3]: b1
Out[3]: <QuerySet [<Book: python_清华大学出版社_20.00_25.00>, <Book: Django_清华大学出版社_70.00_75.00>, <Book: JQury_机械工业出版社_90.00_85.00>, <Book: Linux_机械工业出版社_80.00_65.00>, <Book: HTML5_清华大学出版社_90.00_105.00>]>
按字段取数据
In [3]: b1
Out[3]: <QuerySet [<Book: python_清华大学出版社_20.00_25.00>, <Book: Django_清华大学出版社_70.00_75.00>, <Book: JQury_机械工业出版社_90.00_85.00>, <Book: Linux_机械工业出版社_80.00_65.00>, <Book: HTML5_清华大学出版社_90.00_105.00>]>
In [5]: b1.values('title')
Out[5]: <QuerySet [{'title': 'Django'}, {'title': 'HTML5'}, {'title': 'JQury'}, {'title': 'Linux'}, {'title': 'python'}]>
In [6]: b2=Book.objects.values('title','pub')
In [7]: b2
Out[7]: <QuerySet [{'title': 'python', 'pub': '清华大学出版社'}, {'title': 'Django', 'pub': '清华大学出版社'}, {'title': 'JQury', 'pub': '机械工业出版社'}, {'title': 'Linux', 'pub': '机械工业出版社'}, {'title': 'HTML5', 'pub': '清华大学出版社'}]>
In [9]: for i in b2:
...: print(i['title'],i['pub'])
...:
...:
python 清华大学出版社
Django 清华大学出版社
JQury 机械工业出版社
Linux 机械工业出版社
HTML5 清华大学出版社
In [10]: b3=Book.objects.values_list('title')
In [11]: b3
Out[11]: <QuerySet [('Django',), ('HTML5',), ('JQury',), ('Linux',), ('python',)]>
In [12]: for i in b3:
...: print(i)
...:
('Django',)
('HTML5',)
('JQury',)
('Linux',)
('python',)
In [13]: b4=Book.objects.values_list('title','pub')
In [14]: for i in b4:
...: print(i)
...:
('python', '清华大学出版社')
('Django', '清华大学出版社')
('JQury', '机械工业出版社')
('Linux', '机械工业出版社')
('HTML5', '清华大学出版社')
根据字段排序
In [15]: b5=Book.objects.order_by('-price')
In [16]: b5
Out[16]: <QuerySet [<Book: JQury_机械工业出版社_90.00_85.00>, <Book: HTML5_清华大学出版社_90.00_105.00>, <Book: Linux_机械工业出版社_80.00_65.00>, <Book: Django_清华大学出版社_70.00_75.00>, <Book: python_清华大学出版社_20.00_25.00>]>
针对Queryset对象灵活使用vaules、values_list、order_by
In [17]: b6=Book.objects.values('title').order_by('-price')
In [18]: b6
Out[18]: <QuerySet [{'title': 'JQury'}, {'title': 'HTML5'}, {'title': 'Linux'}, {'title': 'Django'}, {'title': 'python'}]>
queryset query属性获取sql语句
In [19]: b6.query
Out[19]: <django.db.models.sql.query.Query at 0x1ded56f3cd0>
In [20]: print(b6.query)
SELECT `Book`.`title` FROM `Book` ORDER BY `Book`.`price` DESC
views.py
def all_book(request):
all_book=Book.objects.all()
return render(request,'bookstore/all_book.html',locals())
all_book.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>显示所有书籍</title>
</head>
<body>
<table border="1">
<tr>
<th>id</th>
<th>title</th>
<th>pu</th>
<th>price</th>
<th>market_price</th>
<th>op</th>
</tr>
{% for info in all_book %}
<tr>
<td>{{info.id}}</td>
<td>{{info.title}}</td>
<td>{{info.pub}}</td>
<td>{{info.price}}</td>
<td>{{info.market_price}}</td>
<td>
<a href="">更新</a>
<a href="">删除</a>
</td>
</tr>
{% endfor %}
</table>
</body>
</html>
页面显示
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>显示所有书籍</title>
</head>
<body>
<table border="1">
<tr>
<th>id</th>
<th>title</th>
<th>pu</th>
<th>price</th>
<th>market_price</th>
<th>op</th>
</tr>
<tr>
<td>1</td>
<td>python</td>
<td>清华大学出版社</td>
<td>20.00</td>
<td>25.00</td>
<td>
<a href="">更新</a>
<a href="">删除</a>
</td>
</tr>
<tr>
<td>2</td>
<td>Django</td>
<td>清华大学出版社</td>
<td>70.00</td>
<td>75.00</td>
<td>
<a href="">更新</a>
<a href="">删除</a>
</td>
</tr>
<tr>
<td>3</td>
<td>JQury</td>
<td>机械工业出版社</td>
<td>90.00</td>
<td>85.00</td>
<td>
<a href="">更新</a>
<a href="">删除</a>
</td>
</tr>
<tr>
<td>4</td>
<td>Linux</td>
<td>机械工业出版社</td>
<td>80.00</td>
<td>65.00</td>
<td>
<a href="">更新</a>
<a href="">删除</a>
</td>
</tr>
<tr>
<td>5</td>
<td>HTML5</td>
<td>清华大学出版社</td>
<td>90.00</td>
<td>105.00</td>
<td>
<a href="">更新</a>
<a href="">删除</a>
</td>
</tr>
</table>
</body>
</html>