当前位置:网站首页>Unit 6 meet ORM
Unit 6 meet ORM
2022-08-02 14:20:00 【czy1206527605】
1.ORM类型的划分
- BooleanField 布尔类型
- CharField 字符串类型
- DateField 日期类型
- DatetimeField 日期时间类型
- FloatField 浮点类型
- IntegerField 整形
- FileField 文件类型
- ImageField 图片类型
2.字段属性
null 是否允许为空
blank 是否允许为空
PS:nullRuns empty for database contents blankAllow null for form validationchoise 类似于元组set,Choose from tuples
db_column Name the column
db_index 为字段创建索引
default 默认约束
primary_key 主键约束
unique 唯一约束
Table creation case
1.创建模型类
class Bookinfo(models.Model):
name = models.CharField(max_length=20,verbose_name='书名')
pub_date = models.DateField(null=True,verbose_name='发布日期')
read_dount= models.IntegerField(default=0,verbose_name='阅读量')
comment_count = models.IntegerField(default=0,verbose_name='评论量')
sale_out = models.BooleanField(default=False,verbose_name='售空')
2.模型元选项 配置信息
- abstract 不会创建表,Just for other model classes to inherit from
- app_label 指定应用,When a model class does not belong to an application
- db_table 指定表名
- ordering Sorts lists and tuples of data in a table
- verbose_name Specifies the table name for background management
- verbose_name_plural The plural form of the background management table name
class Meta:
verbose_name = '书籍信息表', # adminThe name of the table in the page
verbose_name_plural = verbose_name # adminThe name of the table in the page(复数)
db_table = 'bookinfo' # 在数据库中的表名
def __str__(self): # 直接输出对象时,输出对象的名字
return self.name
3.增删改查
需要用到的命令:python .\manage.py shell
在shellThe execution steps are carried out in the tool
增
# 方式一
Bookinfo.objects.create(
name='python',
pub_date='2022-2-22',
read_dount=500,
comment_count=200,
)
# 方式2
book = Bookinfo(
name='python',
pub_date='2022-04-22',
read_dount=500,
comment_count=100,
)
book.save()
删
# 方式1
Bookinfo.objects.filter(name='python').delete()
# 方式2
book = Bookinfo.objects.get(name='python高级')
book.delete()
改
# 方式1
Bookinfo.objects.filter(name='python').update(sale_out=True)
# 方式2
book = Bookinfo.objects.get(name='python高级')
book.read_dount = 222
book.save()
查
普通查询
# 查询全部信息
Bookinfo.objects.all()
Bookinfo.objects.count()
# 指定查询
Bookinfo.objects.get(name='射雕英雄传')
Bookinfo.objects.filter(name='射雕英雄传')
# 查询编号为1的图书
Bookinfo.objects.get(id=1)
Bookinfo.objects.get(id__exact=1)
# 查询书名包含'湖'的图书
Bookinfo.objects.filter(name__contains='湖')
# 查询书名以'部'结尾的图书
Bookinfo.objects.filter(name__endswith='部')
# 查询书名为空的图书
Bookinfo.objects.filter(name__isnull=True)
# 查询编号为1或3或5的图书
Bookinfo.objects.filter(id__in=[1,3,5])
# 查询编号大于3的图书 gt大于 gte大于等于 lt小于
Bookinfo.objects.filter(id__gt=3)
# 查询1980年发表的图书
Bookinfo.objects.filter(pub_date__year=1980)
# 查询1990年1月1日后发表的图书
Bookinfo.objects.filter(pub_date__gt='1990-01-01')
F,QQuery and aggregate functions
# 查询阅读量大于等于评论量的图书.
from django.db.models import F
Bookinfo.objects.filter(read_dount__gte=F('comment_count'))
# 查询阅读量大于2倍评论量的图书.
Bookinfo.objects.filter(read_dount__gt=F('comment_count')*2)
# 查询阅读量大于20,并且编号小于3的图书.
Bookinfo.objects.filter(read_dount__gt=20,id__lt=3)
Bookinfo.objects.filter(read_dount__gt=20).filter(id__lt=3)
from django.db.models import Q
Bookinfo.objects.filter(Q(read_dount__gt=20) & Q(id__lt=3))
# 查询阅读量大于20,或编号小于3的图书.
Bookinfo.objects.filter(Q(read_dount__gt=20) | Q(id__lt=3))
# 查询编号不等于3的图书.
Bookinfo.objects.exclude(id=3)
Bookinfo.objects.filter(~Q(id=3))
# 查询图书的总阅读量.
from django.db.models import Sum,Max,Min,Avg,Count
Bookinfo.objects.aggregate(Sum('read_dount'))
# 查询图书总数.(The two result types are different)
Bookinfo.objects.count()
Bookinfo.objects.aggregate(Count('id'))
# 查询所有书籍信息平按照阅读量排序.
Bookinfo.objects.all().order_by('read_dount')
Bookinfo.objects.all().order_by('-read_dount')
导包:
from django.db.models import F
from django.db.models import Q
from django.db.models import Sum,Max,Min,Avg,Count
边栏推荐
猜你喜欢
如何解决mysql服务无法启动1069
Chapter6 visualization (don't want to see the version)
【Tensorflow】AttributeError: '_TfDeviceCaptureOp' object has no attribute '_set_device_from_string'
海明校验码纠错设计原理
You can't accept 60% slump, there is no eligible for gain of 6000% in 2021-05-27
专访|带着问题去学习,Apache DolphinScheduler 王福政
第十五单元 分页、过滤
第八单元 中间件
ZABBIX配置邮件报警和微信报警
(ROS) (03) CMakeLists. TXT, rounding
随机推荐
MobileNet ShuffleNet & yolov5替换backbone
Supervision strikes again, what about the market outlook?2021-05-22
【Tensorflow】AttributeError: ‘_TfDeviceCaptureOp‘ object has no attribute ‘_set_device_from_string‘
replay视频播放器_怎么让手机音乐跟视频一起放
瑞吉外卖笔记——第05讲Redis入门
shell脚本“画画”
Sentinel源码(一)SentinelResourceAspect
Basic operations of 8583 sequential stack
About the development forecast of the market outlook?2021-05-23
[ROS] The difference between roscd and cd
You can't accept 60% slump, there is no eligible for gain of 6000% in 2021-05-27
[ROS] The software package of the industrial computer does not compile
The world's largest Apache open source foundation is how it works?
[ROS](03)CMakeLists.txt详解
Haystack的介绍和使用
Flask框架深入一
drf路由组件Routers
文件加密软件有哪些?保障你的文件安全
泡利不相容原理适用的空间范围(系统)是多大?
第五单元 保持状态