文章目录
安装
首先就是安装Mysql和Navicat。
一、数据库配置
其实整个就是连接前端和连后端,因此需要做后端配置,
首先在整体的setting.py 中找到DATABASES,然后修改到自己的配置
python
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'database_demo',
'USER': 'root',
'PASSWORD': 'admin',
'HOST': '127.0.0.1',
'PORT': '3306',
}
然后添加自己的APP
python
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'home',
'databasesdemo'
]
在urls.py中添加自己的App的Url的分段地址,这里是 path('databases/',include("databasesdemo.urls")),前面是上一篇连接前端的
python
# 分段地址
path('movie/',include("movie.urls")),
#########################
path('home/',include("home.urls")),
#########################
path('databases/',include("databasesdemo.urls")),
在APP下的model.py写数据表所需的字段和需求
python
from django.db import models
class BookModel(models.Model):
name = models.CharField(max_length=100)
author = models.CharField(max_length=20)
pub_time = models.DateTimeField(auto_now_add=True)
price = models.FloatField(default=0)
在pycharm 终端中做迁移操作
python
python manage.py makemigrations
执行完这个后会出现
0001_initial.py这个文件
还是在终端做python manage.py migrate映射操作,从而迁移到数据库中
python
python manage.py migrate
二、基本操作步骤
1.增加
在所在App下的view.py中增加代码
python
def add_book(request):
#books=BookModel(name='三国演义',author='罗贯中',price=100)
books = BookModel(name='水浒传', author='施耐庵', price=99)
books.save()
return HttpResponse('图书插入成功')
urls.py连接一下前端路径
python
from django.urls import path
from . import views
#指定应用名称
app_name="databasesdemo"
urlpatterns = [
path("databasebookadd/",views.add_book,name="databasebookadd"),
]
2.查看
在所在App下的view.py中查看代码
python
def query_book(request):
#books=BookModel.objects.all()
#books=BookModel.objects.filter(name='三国演义')
#for book in books:
# print(book.id,book.name,book.author,book.pub_time,book.price)
try:
books = BookModel.objects.get(name='三国演义1')
print(books.name)
except BookModel.DoesNotExist:
print("图书不存在")
return HttpResponse('查找成功')
其中objects.filter和objects...all返回一个数组,因此,修改时候用get,删除时候用filter
urls.py连接一下前端路径
python
path("databasebookquery/",views.query_book,name="databasebookquery"),
3.排序
在所在App下的view.py中查看代码
python
def order_book(request):
books=BookModel.objects.order_by("-pub_time")
for book in books:
print(book.id,book.name,book.author,book.pub_time,book.price)
return HttpResponse('排序成功')
order_by("pub_time")为从小往大正序,order_by("-pub_time")为从大往小的倒序,只多一个负号就行
urls.py连接一下前端路径
python
path("databasebookorder/",views.order_book,name="databasebookorder"),
4.更新
在所在App下的view.py中查看代码
python
def update_book(request):
oldbook=BookModel.objects.get(name='三国演义')
oldbook.name='西游记'
oldbook.save()
return HttpResponse('修改成功')
这里就是get找到,然后替换
urls.py连接一下前端路径
python
path("databasebookupdate/",views.update_book,name="databasebookupdate"),
5.删除数据
代码如下(示例):
python
def delete_book(request):
book=BookModel.objects.filter(name='西游记')
book.delete()
return HttpResponse('删除成功')
urls.py连接一下前端路径
python
path("databasebookdelete/", views.delete_book, name="databasebookdelete"),
三、一对多,多对多,一对一
1.一对多
在model.py中定义两个表其中 Article中author,是User的外键
python
from django.db import models
class User(models.Model):
username = models.CharField(max_length=20)
password = models.CharField(max_length=100)
class Article(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='articles'
在view中定义关系
python
from django.shortcuts import render, HttpResponse
from django.db import connection
from datetime import datetime
from .models import User,Article
# Create your views here.
def article_test(request):
user = User(username='张三',password='111111')
user.save()
# user = User.objects.first()
article = Article(title='ChatGPT6',content='okk',author=user)
article.save()
return HttpResponse("添加成功")
# article=Article.objects.first()
# return HttpResponse(article.author.username)
def one_to_many(request):
user = User.objects.first()
#articles = user.articles.all() # 使用 related_name='articles'
articles = user.articles.filter(title__contains="Chat").all()
for article in articles:
print(article.title)
return HttpResponse("一对多查询成功")
这里可以看到author_id是连接的外键
python
from django.urls import path
from . import views
#指定应用名称
app_name="article"
urlpatterns = [
path("articletest",views.article_test,name="article_test"),
path("one_to_many",views.one_to_many,name="one_to_many"),
]
1.一对一
python
class UserExtension(models.Model):
birthday = models.DateTimeField(null=True)
school=models.CharField(blank=True,max_length=50)
user = models.OneToOneField('User', on_delete=models.CASCADE)
1.多对多
python
class Article(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
author = models.ForeignKey('User', on_delete=models.CASCADE, related_name='articles')
tags = models.ManyToManyField('Tag',related_name='articles')
python
class Tag(models.Model):
name = models.CharField(max_length=100)
四、查询操作
__exact精确查找
__iexact忽略大小写
__contains包含查找
__icontains忽略大小写 包含查找
__in 容器为list迭代查找
__range 范围
python
def query1(request):
# __exact精确查找
#article=Article.objects.filter(id__exact=1)
# __iexact忽略大小写
#article=Article.objects.filter(title__iexact='chatgpt5')
# __contains包含查找
#article=Article.objects.filter(title__contains='GPT')
# __icontains忽略大小写 包含查找
article = Article.objects.filter(title__icontains='gpt')
# __in 容器为list迭代查找
#article = Article.objects.filter(id__in=[1,2,3])
#__range 范围
start_date = datetime(year=2024,month=12,day=20)
end_date = datetime(year=2025, month=1, day=8)
article = Article.objects.filter(pub_time__range=(start_date,end_date))
# 查看执行的语句可以用query
print(article.query)
print(article)
for index in article:
print(index.title)
return HttpResponse('查找成功T')
def query2(request):
# 查找标题中,包含chat的文章的用户
usr=User.objects.filter(articles__title__icontains='chat')
for index in usr:
print(index.id)
print(usr.query)
print(usr)
return HttpResponse('查找成功T')
五、聚合操作
求平均值
result=Book.objects.aggregate(book_avg = Avg('price'))
求个数总和
result = Book.objects.aggregate(book_count = Count('id'))
求最大值最小值
result = Author.objects.aggregate(author_max=Max('age'),author_min=Min('age'))
都是.aggregate都在一个表里面折腾
python
from django.shortcuts import render
from django.db.models import Avg, Count, Max, Min, Sum
from django.shortcuts import render, HttpResponse
from .models import Book,BookOrder,Publisher,Author
# Create your views here.
def aggregate_view(request):
#求平均值
# result=Book.objects.aggregate(book_avg = Avg('price'))
# 求个数总和
# result = Book.objects.aggregate(book_count = Count('id'))
# 求最大值最小值
result = Author.objects.aggregate(author_max=Max('age'),author_min=Min('age'))
print(result)
return HttpResponse('MaxMin_view')
annotate会使用当前这个模型的主键进行分组,bookorder 是模型 BookOrder 的简称,Sum("bookorder__price") 会计算每个 Book 对象的所有相关 BookOrder 对象的 price 字段的总和。
python
def annotate_view(request):
# 求分组总和
result = Book.objects.annotate(total=Sum("bookorder__price")).values('name','total')
print(result)
return HttpResponse('Sum_view')
六、F和Q操作
filter 方法的条件语句需要使用 Q 对象来表示复杂查询(如逻辑或 OR 操作)。直接在 filter 中使用 Python 的 or 关键字是无效的,因为它不会被 Django 解析为 SQL 查询。
python
def q_view(request):
books=Book.objects.filter(Q(price__gte=86)|Q(rating__gte=9)).all()
for book in books:
print(book.name,book.price,book.rating)
return HttpResponse('q_view')
使用 F 对象和 update 方法可以直接在数据库层面进行批量更新,避免了将大量对象加载到内存中进行修改的性能开销。
python
def f_view(request):
Book.objects.update(price=F('price')-10)
return HttpResponse('f_view')