django知识之ORM查询

1. ORM字段

1. AutoField(primary_key=True)      -------->自增且主键
2. CharField(max_length=16)         --------->varchar类型(char类型可以自定义 类改写db_type
3. IntegerField(null=True) --> 最大10位    -------->int类型(最大10位所以手机号码一般用char或者varchar类型)
4. DateField(auto_now_add=True)/DatetimeField(auto_now=True)   ------->相当于(date:年月日,datetime:年月日 小时分钟秒)

    配置auto_now_add=True,创建数据记录的时候会把当前时间添加到数据库;

    配置上auto_now=True,每次更新数据记录的时候会更新该字段
5. EmailField()
6. TextField()     
7. BooleanField()    ------结果是布尔值
8. DecimalField --DecimalField(max_digits =5,decimal_places =2[,**选项]) ----进度是5,其中小数位是2

2. 字段参数

1. null=True --> 可以为空
2. default=100 --> 默认值
3. unique=True --> 唯一
4. db_index=True --> 建立索引


3. 关系字段

1. ForeignKey() --> 外键关联
2. OneToOneField() --> 一对一关联
3. ManyToManyField() --> 多对多

字段参数:
1. to='类名'
2. to_field='属性名'

4. 必知必会13条

1. 返回QuerySet:

1. all()
2. filter()
3. exclude()
4. order_by()
5. reverse()
6. distinct()

7. values() --> 字典的列表
8. values_list() --> 元组的列表

2. 返回具体对象:

1. get()
2. first()
3. last()

3. 返回数字:

1. count()

4. 返回布尔值

1. exists()

5.django中运行测试脚本

 

在app项目下的test.py里面写入一下代码即可

import os

if __name__ == '__main__':
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "orm_demo.settings")
    import django
    django.setup()

    # ORM查询操作
    from app01 import models      #用app01为例
    
    。。。。。测试内容。。。。

5.ORM连表查询

  1 import os
  2 
  3 if __name__ == '__main__':
  4     os.environ.setdefault("DJANGO_SETTINGS_MODULE", "orm_demo.settings")
  5     import django
  6     django.setup()
  7 
  8     # ORM查询操作
  9     from app01 import models
 10 
 11     # 1. 查找所有书名里包含番茄的书
 12     # ret = models.Book.objects.filter(title__contains='番茄')
 13     # print(ret)
 14     # 2. 查找出版日期是2017年的书
 15     # ret = models.Book.objects.filter(publish_date__year=2017)
 16     # print(ret)
 17     # 3. 查找出版日期是2017年的书名
 18     # ret = models.Book.objects.filter(publish_date__year=2017).values("title")
 19     # print(ret)
 20 
 21     # 查找价格大于10元的书
 22     # ret = models.Book.objects.filter(price__gt=10)
 23     # print(ret)
 24 
 25     # 查找价格大于10元的书名和价格
 26     # ret = models.Book.objects.filter(price__gt=10).values("title", "price")
 27     # print(ret)
 28     # 查找memo字段是空的书
 29     # ret = models.Book.objects.filter(memo__isnull=True)
 30     # print(ret)
 31 
 32     # 查找在北京的出版社
 33     # ret = models.Publisher.objects.filter(city='北京')
 34     # print(ret)
 35 
 36     # 查找名字以沙河开头的出版社
 37     # ret = models.Publisher.objects.filter(name__startswith="沙河")
 38     # print(ret)
 39 
 40     # 查找作者名字里面带'小'字的作者
 41     # ret = models.Author.objects.filter(name__contains='小')
 42     # print(ret)
 43 
 44     # 查找年龄大于30岁的作者
 45     # ret = models.Author.objects.filter(age__gt=30)
 46     # print(ret)
 47 
 48     # 查找手机号是155开头的作者
 49     # ret = models.Author.objects.filter(phone__startswith='155')
 50     # print(ret)
 51 
 52     # 查找手机号是155开头的作者的姓名和年龄
 53     # ret = models.Author.objects.filter(phone__startswith='155').values("name", "age")
 54     # print(ret)
 55 
 56     # 连表查询
 57     # book_obj = models.Book.objects.first()
 58     # 基于对象查找
 59     # ret = book_obj.publisher.name
 60     # print(ret)
 61     # 基于双下划线的跨表查询
 62     # ret = models.Book.objects.values("publisher__name", "publisher__city").first()
 63     # print(ret)
 64 
 65     # 正向查找
 66     # 查找书名是“番茄物语”的书的出版社
 67     # 1. 基于对象的查找方式
 68     # ret = models.Book.objects.get(title="番茄物语").publisher
 69     # print(ret)
 70     # 查找书名是“番茄物语”的书的出版社所在的城市
 71     # ret = models.Book.objects.get(title="番茄物语").publisher.city
 72     # print(ret)
 73     # 2. 基于双下划线的夸表查询
 74     # ret = models.Book.objects.filter(title="番茄物语").values("publisher__city")
 75     # print(ret)
 76 
 77     # 外键的反向查找
 78     # 1. 基于对象的查找方式
 79     # publisher_obj = models.Publisher.objects.first()
 80     # ret = publisher_obj.book_set.all()
 81     # print(ret)
 82     # 如果设置了 related_name="books"
 83     # publisher_obj = models.Publisher.objects.first()
 84     # ret = publisher_obj.books.all()
 85     # print(ret)
 86 
 87     # 基于双下划线的跨表查询
 88     # ret = models.Publisher.objects.filter(id=1).values("books__title")
 89     # print(ret)
 90 
 91     # 如果设置了 related_query_name="zhangzhao"
 92     # ret = models.Publisher.objects.filter(id=1).values("zhangzhao__title")
 93     # print(ret)
 94 
 95 
 96     # 多对多操作
 97     # 1. create
 98     # 做了两件事情:
 99     # 1. 创建了一个新的作者,2. 将新创建的作者和第一本书做关联
100     # ret = models.Book.objects.first().author.create(
101     #     name="张曌",
102     #     age=16,
103     #     phone="18012575998",
104     #     detail_id=4
105     # )
106     # ret = models.Book.objects.first().author.all().values("id")
107     # print(ret)
108 
109 
110     # 2. set
111     # models.Book.objects.first().author.set([2,3])
112     # ret = models.Book.objects.first().author.all().values("id")
113     # print(ret)
114 
115     # 3. add
116     # models.Book.objects.first().author.add(1)
117 
118     # ret = models.Book.objects.first().author.all().values("id")
119     # print(ret)
120 
121     # 4. remove
122     # models.Book.objects.first().author.remove(3)
123 
124     # ret = models.Book.objects.first().author.all().values("id")
125     # print(ret)
126 
127     # 5.clear
128     # models.Book.objects.first().author.clear()
129     #
130     # ret = models.Book.objects.first().author.all().values("id")
131     # print(ret)
132 
133     # 6. all
134     # ret = models.Book.objects.first().author.all()
135     # print(ret)
136 
137     # 聚合
138     # 查询所有书的总价格
139     from django.db.models import Avg, Sum, Max, Min, Count
140     #
141     # ret = models.Book.objects.aggregate(total_price=Sum("price"))
142     # print(ret)
143     #
144     # ret = models.Book.objects.aggregate(avg_price=Avg("price"), max_price=Max("price"), min_price=Min("price"))
145     # print(ret)
146 
147 
148     # 求每一本书的作者个数
149     # ret = models.Book.objects.annotate(c=Count("author")).values("title", "c")
150     # print(ret)
151 
152 
153     # 统计出每个出版社买的最便宜的书的价格
154     # ret = models.Publisher.objects.annotate(min_price=Min("books__price")).values("name", "min_price")
155     # print(ret)
156 
157     # 统计不止一个作者的图书 (书作者的数量大于1)
158     # ret = models.Book.objects.annotate(c=Count("author")).filter(c__gt=1)
159     # print(ret)
160 
161     # 按照书作者的数量做排序
162     # ret = models.Book.objects.annotate(c=Count("author")).order_by("c")
163     # print(ret)
164 
165     # 查询各个作者出的书的总价格
166 
167     # ret = models.Author.objects.annotate(sum_price=Sum("book__price")).values("name", "sum_price")
168     # print(ret)
orm连表查询实例测试

猜你喜欢

转载自www.cnblogs.com/dingyutao/p/9210559.html
今日推荐