Django:ORM基本操作-CRUD,管理器对象objects,----->查询3(单条更新,批量更新)
作者:互联网
>>> b1 = Book.objects.get(id=1)
>>> b1
<Book: 书名:python_出版社:清华大学出版社_价格:20.00_市场价:25.00>
>>> b1.price = 22
>>> b1.save()
Microsoft Windows [版本 10.0.19042.928]
(c) Microsoft Corporation。保留所有权利。
C:\Users\520>cd..
C:\Users>cd..
C:\>program files\mysql\mysql server 8.0\bin
'program' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
C:\>cd program files\mysql\mysql server 8.0\bin
C:\Program Files\MySQL\MySQL Server 8.0\bin>mysql -uroot -p
Enter password: ****
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 49
Server version: 8.0.25 MySQL Community Server - GPL
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use mysite; # mysite是project名称
Database changed
mysql> show tables;
+----------------------------+
| Tables_in_mysite |
+----------------------------+
| auth_group |
| auth_group_permissions |
| auth_permission |
| auth_user |
| auth_user_groups |
| auth_user_user_permissions |
| author |
| book |
| django_admin_log |
| django_content_type |
| django_migrations |
| django_session |
+----------------------------+
12 rows in set (0.05 sec)
mysql> desc book
-> ^C
mysql> desc book;
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| id | bigint | NO | PRI | NULL | auto_increment |
| title | varchar(50) | NO | UNI | NULL | |
| price | decimal(7,2) | NO | | NULL | |
| market_price | decimal(7,2) | NO | | NULL | |
| pub | varchar(100) | NO | | NULL | |
+--------------+--------------+------+-----+---------+----------------+
5 rows in set (0.03 sec)
mysql> select * from book;
+----+--------+-------+--------------+----------------+
| id | title | price | market_price | pub |
+----+--------+-------+--------------+----------------+
| 1 | python | 22.00 | 25.00 | 清华大学出版社 |
| 2 | django | 70.00 | 75.00 | 清华大学出版社 |
| 3 | jquery | 90.00 | 85.00 | 机械工业出版社 |
| 4 | linux | 80.00 | 65.00 | 机械工业出版社 |
| 5 | html5 | 90.00 | 105.00 | 清华大学出版社 |
+----+--------+-------+--------------+----------------+
5 rows in set (0.01 sec)
mysql>
>>> books = Book.objects.filter(pub="清华大学出版社")
>>> books.update(price=1)
3
>>> bs = Book.objects.filter(pub="清华大学出版社")
>>> for book in bs:
... print(book.price)
...
1.00
1.00
1.00
>>>
mysql> select * from book;
+----+--------+-------+--------------+----------------+
| id | title | price | market_price | pub |
+----+--------+-------+--------------+----------------+
| 1 | python | 1.00 | 25.00 | 清华大学出版社 |
| 2 | django | 1.00 | 75.00 | 清华大学出版社 |
| 3 | jquery | 90.00 | 85.00 | 机械工业出版社 |
| 4 | linux | 80.00 | 65.00 | 机械工业出版社 |
| 5 | html5 | 1.00 | 105.00 | 清华大学出版社 |
+----+--------+-------+--------------+----------------+
5 rows in set (0.00 sec)
mysql>
总路由
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path("bookstore/", include("myapp.urls"))
]
子路由
from django.urls import path
from . import views
urlpatterns = [
path("all_book", views.all_book),
path("update_book/<int:book_id>", views.update_book)
]
myapp.views.py
from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect
from .models import Book
# Create your views here.
def all_book(request):
all_book = Book.objects.all()
return render(request, "myapp/abook.html", locals())
def update_book(request, book_id):
# 查
try:
book = Book.objects.get(id=book_id)
except Exception as e:
print("update book error is {}".format(e))
return HttpResponse("book {} does not exist".format(book_id))
if request.method == "GET":
return render(request, "myapp/update_book.html", locals())
elif request.method == "POST":
price = request.POST['price']
market_price = request.POST['market_price']
# 改
book.price = price
book.market_price = market_price
# 存
book.save()
return HttpResponseRedirect("/bookstore/all_book") # 注意:相对地址
abook.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>查看所有书籍</title>
</head>
<body>
<table border="1">
<tr>
<th>id</th>
<th>title</th>
<th>pub</th>
<th>price</th>
<th>market_price</th>
<th>op</th>
</tr>
<tr>
{% for book in all_book %}
<tr>
<td>{{ book.id }}</td>
<td>{{ book.title }}</td>
<td>{{ book.pub }}</td>
<td>{{ book.price }}</td>
<td>{{ book.market_price }}</td>
<td>
<a href="/bookstore/update_book/{{ book.id }}">更新</a>
<a href="">删除</a>
</td>
</tr>
{% endfor %}
</tr>
</table>
</body>
</html>
update_book.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>更新书籍</title>
</head>
<body>
<form action="/bookstore/update_book/{{ book_id }}" method="post">
{% csrf_token %}
<!--这里提交到的URL要注意,使用相对地址,最前面的斜杠不要丢-->
<p>
title<input type="text" value="{{ book.title }}" disabled="disabled">
</p> <!--没有给name,点击提交时不会提交这个值。disabled属性,值不可修改 -->
<p>
pub<input type="text" value="{{ book.pub }}" disabled="disabled">
</p>
<p>
price<input type="text" name="price" value="{{ book.price }}">
</p>
<p>
market_price<input type="text" name="market_price" value="{{ book.market_price }}">
</p>
<p>
<input type="submit" value="更新啦">
</p>
</form>
</body>
</html>
标签:管理器,price,更新,django,book,mysql,基本操作,id,market 来源: https://blog.csdn.net/weixin_47008635/article/details/117718693