不做大哥好多年 不做大哥好多年
首页
  • MySQL
  • Redis
  • Elasticsearch
  • Kafka
  • Etcd
  • MongoDB
  • TiDB
  • RabbitMQ
  • 01.GO基础
  • 02.面向对象
  • 03.并发编程
  • 04.常用库
  • 05.数据库操作
  • 06.Beego框架
  • 07.Beego商城
  • 08.GIN框架
  • 09.GIN论坛
  • 10.微服务
  • 01.Python基础
  • 02.Python模块
  • 03.Django
  • 04.Flask
  • 05.SYL
  • 06.Celery
  • 10.微服务
  • 01.Java基础
  • 02.面向对象
  • 03.Java进阶
  • 04.Web基础
  • 05.Spring框架
  • 100.微服务
  • Docker
  • K8S
  • 容器原理
  • Istio
  • 数据结构
  • 算法基础
  • 算法题分类
  • 前置知识
  • PyTorch
  • 01.Python
  • 02.GO
  • 03.Java
  • 04.业务问题
  • 05.关键技术
  • 06.项目常识
  • 10.计算机基础
  • Linux基础
  • Linux高级
  • Nginx
  • KeepAlive
  • ansible
  • zabbix
  • Shell
  • Linux内核

逍遥子

不做大哥好多年
首页
  • MySQL
  • Redis
  • Elasticsearch
  • Kafka
  • Etcd
  • MongoDB
  • TiDB
  • RabbitMQ
  • 01.GO基础
  • 02.面向对象
  • 03.并发编程
  • 04.常用库
  • 05.数据库操作
  • 06.Beego框架
  • 07.Beego商城
  • 08.GIN框架
  • 09.GIN论坛
  • 10.微服务
  • 01.Python基础
  • 02.Python模块
  • 03.Django
  • 04.Flask
  • 05.SYL
  • 06.Celery
  • 10.微服务
  • 01.Java基础
  • 02.面向对象
  • 03.Java进阶
  • 04.Web基础
  • 05.Spring框架
  • 100.微服务
  • Docker
  • K8S
  • 容器原理
  • Istio
  • 数据结构
  • 算法基础
  • 算法题分类
  • 前置知识
  • PyTorch
  • 01.Python
  • 02.GO
  • 03.Java
  • 04.业务问题
  • 05.关键技术
  • 06.项目常识
  • 10.计算机基础
  • Linux基础
  • Linux高级
  • Nginx
  • KeepAlive
  • ansible
  • zabbix
  • Shell
  • Linux内核
  • python基础

  • python模块

  • django

    • 01_Django基础

    • 02_Model数据库操作

      • 01.配置数据库
      • 02.定义模型
      • 03.一对多
      • 04.多对多
      • 05.一大波Model操作
        • 01.基本操作
        • 02.进阶操作
        • 03.时间过滤
      • 06.F()和Q()查询
      • 07.aggregate和annotate
      • 08.django-admin
    • 03_DRF框架

    • 04_部署

  • flask

  • SYL

  • Celery

  • 微服务

  • python
  • django
  • 02_Model数据库操作
xiaonaiqiang
2021-02-16
目录

05.一大波Model操作

# 01.基本操作

from django.shortcuts import HttpResponse
from app01 import models

def orm(request):
    # 1 创建
    # 创建数据方法一
    models.UserInfo.objects.create(username='root', password='123')
    # 创建数据方法二
    obj = models.UserInfo(username='alex', password='123')
    obj.save()
    # 创建数据库方法三(传入字典必须在字典前加两个星号)
    dic = {'username': 'eric', 'password': '666'}
    models.UserInfo.objects.create(**dic)

    # 2 查
    result = models.UserInfo.objects.all()  # 查找所有条目
    result = models.UserInfo.objects.filter(username='alex', password='123')
    for row in result:
        print(row.id, row.username, row.password)

    # 3 删除
    models.UserInfo.objects.all().delete()  # 删除所有
    models.UserInfo.objects.filter(username='alex').delete()  # 删除指定

    # 4 更新
    models.UserInfo.objects.all().update(password='12345')
    models.UserInfo.objects.filter(id=4).update(password='15')

    # 5 获取个数
    models.UserInfo.objects.filter(name='seven').count()

    # 6 执行原生SQL
    # 6.1 执行原生SQL
    models.UserInfo.objects.raw('select * from userinfo')

    # 6.2 如果SQL是其他表时,必须将名字设置为当前UserInfo对象的主键列名
    models.UserInfo.objects.raw('select id as nid from 其他表')

    # 6.3 指定数据库
    models.UserInfo.objects.raw('select * from userinfo', using="default")

    return HttpResponse('orm')
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

# 02.进阶操作

  • 进阶操作:牛掰掰的双下划线
1、大于,小于
        # models.Tb1.objects.filter(id__gt=1)                 # 获取id大于1的值
        # models.Tb1.objects.filter(id__gte=1)                # 获取id大于等于1的值
        # models.Tb1.objects.filter(id__lt=10)                # 获取id小于10的值
        # models.Tb1.objects.filter(id__lte=10)               # 获取id小于10的值
        # models.Tb1.objects.filter(id__lt=10, id__gt=1)      # 获取id大于1 且 小于10的值
2、in
        # models.Tb1.objects.filter(id__in=[11, 22, 33])      # 获取id等于11、22、33的数据
        # models.Tb1.objects.exclude(id__in=[11, 22, 33])     # not in
3、isnull
        # Entry.objects.filter(pub_date__isnull=True)         #双下划线isnull,查找pub_date是null的数据
4、contains                                                    #就是原生sql的like操作:模糊匹配
        # models.Tb1.objects.filter(name__contains="ven")
        # models.Tb1.objects.filter(name__icontains="ven")    # icontains大小写不敏感
        # models.Tb1.objects.exclude(name__icontains="ven")
5、range
        # models.Tb1.objects.filter(id__range=[1, 2])          # 范围bettwen and
6、order by
        # models.Tb1.objects.filter(name='seven').order_by('id')      # asc     没有减号升续排列
        # models.Tb1.objects.filter(name='seven').order_by('-id')     # desc      有减号升续排列
7、group by
        # from django.db.models import Count, Min, Max, Sum
        # models.Tb1.objects.filter(c1=1).values('id').annotate(c=Count('num'))        #根据id列进行分组
        # SELECT "app01_tb1"."id", COUNT("app01_tb1"."num") AS "c" FROM "app01_tb1" 
        # WHERE "app01_tb1"."c1" = 1 GROUP BY "app01_tb1"."id"
8、limit 、offset    #分页
        # models.Tb1.objects.all()[10:20]
9、regex正则匹配,iregex 不区分大小写
        # Entry.objects.get(title__regex=r'^(An?|The) +')
        # Entry.objects.get(title__iregex=r'^(an?|the) +')

        
10、date
        # Entry.objects.filter(pub_date__date=datetime.date(2005, 1, 1))          #__data表示日期查找,2005-01-01
        # Entry.objects.filter(pub_date__date__gt=datetime.date(2005, 1, 1))
11、year
        # Entry.objects.filter(pub_date__year=2005)                               #__year根据年查找
        # Entry.objects.filter(pub_date__year__gte=2005)
12、month
        # Entry.objects.filter(pub_date__month=12)
        # Entry.objects.filter(pub_date__month__gte=6)
13、day
        # Entry.objects.filter(pub_date__day=3)
        # Entry.objects.filter(pub_date__day__gte=3)
14、week_day
        # Entry.objects.filter(pub_date__week_day=2)
        # Entry.objects.filter(pub_date__week_day__gte=2)
15、hour
        # Event.objects.filter(timestamp__hour=23)
        # Event.objects.filter(time__hour=5)
        # Event.objects.filter(timestamp__hour__gte=12)
16、minute
        # Event.objects.filter(timestamp__minute=29)
        # Event.objects.filter(time__minute=46)
        # Event.objects.filter(timestamp__minute__gte=29)
17、second
        # Event.objects.filter(timestamp__second=31)
        # Event.objects.filter(time__second=2)
        # Event.objects.filter(timestamp__second__gte=31)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

# 03.时间过滤

  • 根据天/小时进行过滤
from django.utils import timezone
from report.models import *

now = timezone.now()
# start_time = now - timezone.timedelta(days=7)
start_time = now - timezone.timedelta(hours=240)  # 查询10天前的数据
end_time = now

qs = AllClarm.objects.filter(start_tm__range=(start_time, end_time))
1
2
3
4
5
6
7
8
9
上次更新: 2024/3/13 15:35:10
04.多对多
06.F()和Q()查询

← 04.多对多 06.F()和Q()查询→

最近更新
01
04.数组双指针排序_子数组
03-25
02
08.动态规划
03-25
03
06.回溯算法
03-25
更多文章>
Theme by Vdoing | Copyright © 2019-2025 逍遥子 技术博客 京ICP备2021005373号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式