Django ORM操作
看专业的官网文档,做专业的程序员!:https://docs.djangoproject.com/en/1.11/ref/models/querysets/
必知必会13条
<1> all(): #查询所有结果
models.Person.objects # app01.Person.objects <class 'django.db.models.manager.Manager'>
<2> filter(**kwargs): #它包含了与所给筛选条件相匹配的对象 <3> get(**kwargs): #返回与所给筛选条件相匹配的对象,返回结果有且只有一个,如果符合筛选条件的对象超过一个或者没有都会抛出错误。 <4> exclude(**kwargs): #它包含了与所给筛选条件不匹配的对象 <5> values(*field): #返回一个ValueQuerySet——一个特殊的QuerySet,运行后得到的并不是一系列model的实例化对象,而是一个可迭代的字典序列 <6> values_list(*field): #它与values()非常相似,它返回的是一个元组序列,values返回的是一个字典序列 <7> order_by(*field): #对查询结果排序 <8> reverse(): #对查询结果反向排序,请注意reverse()通常只能在具有已定义顺序的QuerySet上调用(在model类的Meta中指定ordering或调用order_by()方法)。 <9> distinct(): #从返回结果中剔除重复纪录(如果你查询跨越多个表,可能在计算QuerySet时得到重复的结果。此时可以使用distinct(), #注意只有在PostgreSQL中支持按字段去重。) <10> count(): #返回数据库中匹配查询(QuerySet)的对象数量。管理对象也能用(如:models.Person.objects) <11> first(): #返回第一条记录 <12> last(): #返回最后一条记录 <13> exists(): #如果QuerySet包含数据,就返回True,否则返回False
返回QuerySet对象的方法有
all()
filter()
exclude()
order_by()
reverse()
distinct()
特殊的QuerySet
values() 返回一个可迭代的字典序列
values_list() 返回一个可迭代的元祖序列
返回具体对象的
get()
first()
last()
返回布尔值的方法有:
exists()
返回数字的方法有
count()
单表查询之神奇的双下划线
models.Tb1.objects.filter(id__lt=10, id__gt=1) # 获取id大于1 且 小于10的值 models.Tb1.objects.filter(id__in=[11, 22, 33]) # 获取id等于11、22、33的数据 models.Tb1.objects.exclude(id__in=[11, 22, 33]) # not in models.Tb1.objects.filter(name__contains="ven") # 获取name字段包含"ven"的 models.Tb1.objects.filter(name__icontains="ven") # icontains大小写不敏感 models.Tb1.objects.filter(id__range=[1, 3]) # id范围是1到3的,等价于SQL的bettwen and ret=models.Person.objects.filter(name__isnull=False) # 筛选内容为空/内容不为 date字段还可以: models.Person.objects.filter(birth__year='2018') # 筛选2018年出生的,birth为字段,字段内容为年月日 models.Person.objects.filter(birth__month='05') models.Person.objects.filter(birth__day='12') ret=models.Person.objects.filter(name__isnull=False) # 筛选内容为空/内容不为
ForeignKey操作
正向查找
对象查找(跨表)
语法:
对象.关联字段.字段
示例:
book_obj = models.Book.objects.first() # 第一本书对象 print(book_obj.publisher) # 得到这本书关联的出版社对象 print(book_obj.publisher.name) # 得到出版社对象的名称
字段查找(跨表)
语法:
关联字段__字段
示例:
print(models.Book.objects.values_list("publisher__name"))
反向操作
对象查找
语法:
obj.表名_set
示例:
publisher_obj = models.Publisher.objects.first() # 找到第一个出版社对象 books = publisher_obj.book_set.all() # 找到第一个出版社出版的所有书 titles = books.values_list("title") # 找到第一个出版社出版的所有书的书名
字段查找
语法:
表名__字段
示例:
titles = models.Publisher.objects.values_list("book__title")
实例
# 正向查找 # 基于对象查找 对象.关联字段.字段 book_obj = models.Book.objects.first() # print(book_obj) # <Book obj:17 光头强伐木记> # print(book_obj.publisher) # <Person obj:1 熊出没出版社> # print(book_obj.publisher.name) # 熊出没出版社 ret = book_obj.publisher_id ret = book_obj.publisher.id # 基于字段查找 关联字段__字段 pub_name = models.Book.objects.values_list('publisher__name') # print(pub_name) # <QuerySet [('熊出没出版社',), ('熊出没出版社',), ('熊出没出版社',), ('火影出版社',), ('火影出版社',), ('王府井出版社',),...]> ret = models.Book.objects.filter(publisher__name='熊出没出版社') # 查询出版社=熊出没出版社的所有书籍 ret = models.Book.objects.filter(publisher_id=1) # 通过外键直接查的book # 反向操作(以少查多) # related_name=‘xxx‘: 反向:小写表名_set ===>xxx(推荐使用) # related_query_name = ‘xxx‘ 反向 :小写表名_set == > xxx_set # 基于对象查找 obj.表名_set--->管理对象 obj.表名_set.all()出版社关联的所有书籍的对象 pub_obj = models.Publisher.objects.first() # ret = pub_obj.book_set # <locals>.RelatedManager' # ret = pub_obj.book_set.all() # <QuerySet [<Book: <Book obj:17 光头强伐木记>>, <Book: <Book obj:18 保护森林>>, <Book: <Book obj:19 熊大熊二相亲记>>]> # ret=pub_obj.book_set.all().values_list('name') # 找到第一个出版社出版的所有书的书名 # ret = pub_obj.pub_books.all().values_list('name') # 设置了related_name='pub_books'后,就不能用表名小写_set了 # 基于字段查找 表名__字段 # ret=models.Publisher.objects.values("book__name") # 字典形式 # ret=models.Publisher.objects.values_list("book__name") # 元祖形式,只有值 # ret=models.Publisher.objects.values_list("book__name") # 元祖形式,只有值 ret=models.Publisher.objects.values_list("name") # 元祖形式,只有值 # ret=models.Publisher.objects.values_list("bk__name") # 设置了 related_query_name='bk',后,就不能用表名小写__set了 print(ret, type(ret))
related_name,related_query_name,default_related_name的比较
django 默认每个主表的对象都有一个是外键的属性,可以通过它来查询到所有属于主表的子表的信息。 这个属性的名称默认是以子表的名称小写加上_set()来表示,默认返回的是一个querydict对象,你可以继续的根据情况来查询等操作。 在实际项目中,我们使用最多的还是related_name 如果你觉得上面的定义比较麻烦的话,你也可以在定义主表的外键的时候,给这个外键定义好一个名称。要用related_name比如在Book表中: publisher = models.ForeignKey(Person, related_name='pub_books') 那么实现上面的需求,可以使用person.book_set.all(),也可以使用person.pub_books.all() 1) default_related_name在class Meta中设置, related_name和related_query_name在外键字段中作为属性设置 2) 反向查询可通过对象进行查询,也可通过filter或values进行查询 3)通过对象查询时,使用related_name;如果related_name未设置,使用default_related_name 4)通过filter或values进行查询时,使用related_query_name related_query_name的默认值是 related_name(如果设置(自定义)了related_name),否则 related_query_name的默认值是default_related_name(如果设置(自定义)了default_related_name),否则 related_query_name的默认值是model_name
回顾以往例子:反向查找别名替换
class UserType(models.Model): title = models.CharField(max_length=32) class User(models.Model): username = models.CharField(max_length=32) ut = models.ForeignKey(‘UserType‘,related_name=‘xxx‘) ## related_name=‘xxx‘: 反向:小写表名user_set ===>xxx ## related_query_name = ‘xxx‘ 反向:user_set==>xxx_set 推荐用: ut = models.ForeignKey(‘UserType‘,related_name=‘users‘) obj.users
ManyToManyField
class RelatedManager
"关联管理器"是在一对多或者多对多的关联上下文中使用的管理器。
它存在于下面两种情况:
- 外键关系的反向查询
- 多对多关联关系
简单来说就是当 点后面的对象 可能存在多个的时候就可以使用以下的方法。
方法
create()
创建一个新的对象,保存对象,并将它添加到关联对象集之中,返回新创建的对象。
>>> import datetime >>> models.Author.objects.first().book_set.create(title="番茄物语", publish_date=datetime.date.today())
add()
把指定的model对象添加到关联对象集中。
添加对象
>>> author_objs = models.Author.objects.filter(id__lt=3) >>> models.Book.objects.first().authors.add(*author_objs)
添加id
>>> models.Book.objects.first().authors.add(*[1, 2])
set()
更新model对象的关联对象。
>>> book_obj = models.Book.objects.first() >>> book_obj.authors.set([2, 3])
remove()
从关联对象集中移除执行的model对象
>>> book_obj = models.Book.objects.first() >>> book_obj.authors.remove(3)
clear()
从关联对象集中移除一切对象。
>>> book_obj = models.Book.objects.first() >>> book_obj.authors.clear()
注意:
对于ForeignKey对象,clear()和remove()方法仅在null=True时存在。
举个例子:
ForeignKey字段没设置null=True时,
class Book(models.Model): title = models.CharField(max_length=32) publisher = models.ForeignKey(to=Publisher)
没有clear()和remove()方法:
>>> models.Publisher.objects.first().book_set.clear() Traceback (most recent call last): File "<input>", line 1, in <module> AttributeError: 'RelatedManager' object has no attribute 'clear'
当ForeignKey字段设置null=True时,
class Book(models.Model): name = models.CharField(max_length=32) publisher = models.ForeignKey(to=Class, null=True)
此时就有clear()和remove()方法:
>>> models.Publisher.objects.first().book_set.clear()
注意:
- 对于所有类型的关联字段,add()、create()、remove()和clear(),set()都会马上更新数据库。换句话说,在关联的任何一端,都不需要再调用save()方法。
实例
author_obj=models.Author.objects.get(id=1) # author_obj=models.Author.objects.get(id=1).books.all() # print(author_obj,type(author_obj)) # <Author obj:1 名人> <class 'app01.models.Author'> # create # author_obj.books.create(name='尾兽玉螺旋丸',publisher_id=1) # author_obj.books.create(name='千年杀',publisher_id=2) # 1.创建书籍对象(与出版社关联) # 2.该对象和作者关联 # print(author_obj.books.all(), type(author_obj.books.all())) # pub_obj=models.Publisher.objects.first() ## pub_obj.book_set # pub_obj.pub_books.create(name='小花历险记') # 此时book_set效果等同于pub_books # pub_obj.book_set.create(name='小花历险记') # 此时会报错'Publisher' object has no attribute 'book_set' # add # 添加对象 books_obj=models.Book.objects.filter(id__in=[28,25]) # author_obj.books.add(*books_obj) # 给id为1的作者添加25,28两本书,必须打散 # print(books_obj) # 添加id author_obj2 = models.Author.objects.get(id=2) # author_obj2.books.add(*[31,32]) # 给id为2的作者添加31,32两本书,*[31,32]必须打散 # print(author_obj2.books.all(), type(author_obj2.books.all())) # set 更新model对象的关联对象 # id # author_obj.books.set([20,21,24]) # 对象 books_obj2 = models.Book.objects.filter(id__in=[21,22,23]) # author_obj2.books.set(books_obj2) # remove 从关联对象集中移除执行的model对象 # 外键 一对多的管理对象remove不能使用id 使用对象 # 对于ForeignKey对象,clear()和remove()方法仅在null=True时存在。否则报错 # 'Publisher' object has no attribute 'book' pub_obj=models.Publisher.objects.get(id=1) pub_obj.pub_books.remove(models.Book.objects.get(id=17)) # 外键 # clear 从关联对象集中移除一切对象。 pub_obj.pub_books.clear() # 多对多 # author_obj2.books.remove(*books_obj2) # print(author_obj.books.all(), type(author_obj.books.all())) # print(author_obj2.books.all(), type(author_obj2.books.all()))
聚合查询和分组查询
聚合
aggregate()是QuerySet 的一个终止子句,意思是说,它返回一个包含一些键值对的字典。
键的名称是聚合值的标识符,值是计算出来的聚合值。键的名称是按照字段和聚合函数的名称自动生成出来的。
用到的内置函数:
from django.db.models import Avg, Sum, Max, Min, Count
示例:
>>> from django.db.models import Avg, Sum, Max, Min, Count >>> models.Book.objects.all().aggregate(Avg("price")) {'price__avg': 13.233333}
如果你想要为聚合值指定一个名称,可以向聚合子句提供它。
>>> models.Book.objects.aggregate(average_price=Avg('price')) {'average_price': 13.233333}
如果你希望生成不止一个聚合,你可以向aggregate()子句中添加另一个参数。所以,如果你也想知道所有图书价格的最大值和最小值,可以这样查询:
>>> models.Book.objects.all().aggregate(Avg("price"), Max("price"), Min("price")) {'price__avg': 13.233333, 'price__max': Decimal('19.90'), 'price__min': Decimal('9.90')}
分组
我们在这里先复习一下SQL语句的分组。
假设现在有一张公司职员表:
我们使用原生SQL语句,按照部分分组求平均工资:
select dept,AVG(salary) from employee group by dept;
ORM查询:
from django.db.models import Avg Employee.objects.values("dept").annotate(avg=Avg("salary").values("dept", "avg")
连表查询的分组:
SQL查询:
select dept.name,AVG(salary) from employee inner join dept on (employee.dept_id=dept.id) group by dept_id;
ORM查询:
from django.db.models import Avg models.Dept.objects.annotate(avg=Avg("employee__salary")).values("name", "avg")
更多示例:
示例1:统计每一本书的作者个数
>>> book_list = models.Book.objects.all().annotate(author_num=Count("author")) >>> for obj in book_list: ... print(obj.author_num) ... 2 1 1
示例2:统计出每个出版社买的最便宜的书的价格
>>> publisher_list = models.Publisher.objects.annotate(min_price=Min("book__price")) >>> for obj in publisher_list: ... print(obj.min_price) ... 9.90 19.90
方法二:
>>> models.Book.objects.values("publisher__name").annotate(min_price=Min("price")) <QuerySet [{'publisher__name': '沙河出版社', 'min_price': Decimal('9.90')},
{'publisher__name': '人民出版社', 'min_price': Decimal('19.90')}]>
示例3:统计不止一个作者的图书
>>> models.Book.objects.annotate(author_num=Count("author")).filter(author_num__gt=1) <QuerySet [<Book: 番茄物语>]>
示例4:根据一本图书作者数量的多少对查询集 QuerySet进行排序
>>> models.Book.objects.annotate(author_num=Count("author")).order_by("author_num") <QuerySet [<Book: 香蕉物语>, <Book: 橘子物语>, <Book: 番茄物语>]>
示例5:查询各个作者出的书的总价格
>>> models.Author.objects.annotate(sum_price=Sum("book__price")).values("name", "sum_price") <QuerySet [{'name': '小精灵', 'sum_price': Decimal('9.90')}, {'name': '小仙女', 'sum_price': Decimal('29.80')}, {'name': '小魔女', 'sum_price': Decimal('9.90')}]>
F查询和Q查询
F查询
在上面所有的例子中,我们构造的过滤器都只是将字段值与某个常量做比较。如果我们要对两个字段的值做比较,那该怎么做呢?
Django 提供 F() 来做这样的比较。F() 的实例可以在查询中引用字段,来比较同一个 model 实例中两个不同字段的值。
示例1:
查询评论数大于收藏数的书籍
from django.db.models import F
models.Book.objects.filter(commnet_num__gt=F('keep_num'))
Django 支持 F() 对象之间以及 F() 对象和常数之间的加减乘除和取模的操作。
models.Book.objects.filter(commnet_num__lt=F('keep_num')*2)
修改操作也可以使用F函数,比如将每一本书的价格提高30元
models.Book.objects.all().update(price=F("price")+30)
引申:
如果要修改char字段咋办?
如:把所有书名后面加上(第一版)
>>> from django.db.models.functions import Concat >>> from django.db.models import Value >>> models.Book.objects.all().update(title=Concat(F("title"), Value("("), Value("第一版"), Value(")")))
Q查询
filter() 等方法中的关键字参数查询都是一起进行“AND” 的。 如果你需要执行更复杂的查询(例如OR语句),你可以使用Q对象。
示例1:
查询作者名是小仙女或小魔女的
models.Book.objects.filter(Q(authors__name="小仙女")|Q(authors__name="小魔女"))
你可以组合& 和| 操作符以及使用括号进行分组来编写任意复杂的Q 对象。同时,Q 对象可以使用~ 操作符取反,这允许组合正常的查询和取反(NOT) 查询。
示例:查询作者名字是小仙女并且不是2018年出版的书的书名。
>>> models.Book.objects.filter(Q(author__name="小仙女") & ~Q(publish_date__year=2018)).values_list("title") <QuerySet [('番茄物语',)]>
查询函数可以混合使用Q 对象和关键字参数。所有提供给查询函数的参数(关键字参数或Q 对象)都将"AND”在一起。但是,如果出现Q 对象,它必须位于所有关键字参数的前面。
例如:查询出版年份是2017或2018,书名中带物语的所有书。
>>> models.Book.objects.filter(Q(publish_date__year=2018) | Q(publish_date__year=2017), title__icontains="物语") <QuerySet [<Book: 番茄物语>, <Book: 香蕉物语>, <Book: 橘子物语>]>
事务
import os if __name__ == '__main__': os.environ.setdefault("DJANGO_SETTINGS_MODULE", "BMS.settings") import django django.setup() import datetime from app01 import models try: from django.db import transaction with transaction.atomic(): new_publisher = models.Publisher.objects.create(name="火星出版社") models.Book.objects.create(title="橘子物语", publish_date=datetime.date.today(), publisher_id=10) # 指定一个不存在的出版社id except Exception as e: print(str(e))
其他鲜为人知的操作(有个印象即可)
Django ORM执行原生SQL
ORM 执行原生SQL的方法
# extra # 在QuerySet的基础上继续执行子语句 # extra(self, select=None, where=None, params=None, tables=None, order_by=None, select_params=None) # select和select_params是一组,where和params是一组,tables用来设置from哪个表 # Entry.objects.extra(select={'new_id': "select col from sometable where othercol > %s"}, select_params=(1,)) # Entry.objects.extra(where=['headline=%s'], params=['Lennon']) # Entry.objects.extra(where=["foo='a' OR bar = 'a'", "baz = 'a'"]) # Entry.objects.extra(select={'new_id': "select id from tb where id > %s"}, select_params=(1,), order_by=['-nid']) 举个例子: models.UserInfo.objects.extra( select={'newid':'select count(1) from app01_usertype where id>%s'}, select_params=[1,], where = ['age>%s'], params=[18,], order_by=['-age'], tables=['app01_usertype'] ) """ select app01_userinfo.id, (select count(1) from app01_usertype where id>1) as newid from app01_userinfo,app01_usertype where app01_userinfo.age > 18 order by app01_userinfo.age desc """ # 执行原生SQL # 更高灵活度的方式执行原生SQL语句 # from django.db import connection, connections # cursor = connection.cursor() # cursor = connections['default'].cursor() # cursor.execute("""SELECT * from auth_user where id = %s""", [1]) # row = cursor.fetchone()
QuerySet方法大全
QuerySet方法大全
################################################################## # PUBLIC METHODS THAT ALTER ATTRIBUTES AND RETURN A NEW QUERYSET # ################################################################## def all(self) # 获取所有的数据对象 def filter(self, *args, **kwargs) # 条件查询 # 条件可以是:参数,字典,Q def exclude(self, *args, **kwargs) # 条件查询 # 条件可以是:参数,字典,Q def select_related(self, *fields) 性能相关:表之间进行join连表操作,一次性获取关联的数据。 总结: 1. select_related主要针一对一和多对一关系进行优化。 2. select_related使用SQL的JOIN语句进行优化,通过减少SQL查询的次数来进行优化、提高性能。 def prefetch_related(self, *lookups) 性能相关:多表连表操作时速度会慢,使用其执行多次SQL查询在Python代码中实现连表操作。 总结: 1. 对于多对多字段(ManyToManyField)和一对多字段,可以使用prefetch_related()来进行优化。 2. prefetch_related()的优化方式是分别查询每个表,然后用Python处理他们之间的关系。 def annotate(self, *args, **kwargs) # 用于实现聚合group by查询 from django.db.models import Count, Avg, Max, Min, Sum v = models.UserInfo.objects.values('u_id').annotate(uid=Count('u_id')) # SELECT u_id, COUNT(ui) AS `uid` FROM UserInfo GROUP BY u_id v = models.UserInfo.objects.values('u_id').annotate(uid=Count('u_id')).filter(uid__gt=1) # SELECT u_id, COUNT(ui_id) AS `uid` FROM UserInfo GROUP BY u_id having count(u_id) > 1 v = models.UserInfo.objects.values('u_id').annotate(uid=Count('u_id',distinct=True)).filter(uid__gt=1) # SELECT u_id, COUNT( DISTINCT ui_id) AS `uid` FROM UserInfo GROUP BY u_id having count(u_id) > 1 def distinct(self, *field_names) # 用于distinct去重 models.UserInfo.objects.values('nid').distinct() # select distinct nid from userinfo 注:只有在PostgreSQL中才能使用distinct进行去重 def order_by(self, *field_names) # 用于排序 models.UserInfo.objects.all().order_by('-id','age') def extra(self, select=None, where=None, params=None, tables=None, order_by=None, select_params=None) # 构造额外的查询条件或者映射,如:子查询 Entry.objects.extra(select={'new_id': "select col from sometable where othercol > %s"}, select_params=(1,)) Entry.objects.extra(where=['headline=%s'], params=['Lennon']) Entry.objects.extra(where=["foo='a' OR bar = 'a'", "baz = 'a'"]) Entry.objects.extra(select={'new_id': "select id from tb where id > %s"}, select_params=(1,), order_by=['-nid']) def reverse(self): # 倒序 models.UserInfo.objects.all().order_by('-nid').reverse() # 注:如果存在order_by,reverse则是倒序,如果多个排序则一一倒序 def defer(self, *fields): models.UserInfo.objects.defer('username','id') 或 models.UserInfo.objects.filter(...).defer('username','id') #映射中排除某列数据 def only(self, *fields): #仅取某个表中的数据 models.UserInfo.objects.only('username','id') 或 models.UserInfo.objects.filter(...).only('username','id') def using(self, alias): 指定使用的数据库,参数为别名(setting中的设置) ################################################## # PUBLIC METHODS THAT RETURN A QUERYSET SUBCLASS # ################################################## def raw(self, raw_query, params=None, translations=None, using=None): # 执行原生SQL models.UserInfo.objects.raw('select * from userinfo') # 如果SQL是其他表时,必须将名字设置为当前UserInfo对象的主键列名 models.UserInfo.objects.raw('select id as nid from 其他表') # 为原生SQL设置参数 models.UserInfo.objects.raw('select id as nid from userinfo where nid>%s', params=[12,]) # 将获取的到列名转换为指定列名 name_map = {'first': 'first_name', 'last': 'last_name', 'bd': 'birth_date', 'pk': 'id'} Person.objects.raw('SELECT * FROM some_other_table', translations=name_map) # 指定数据库 models.UserInfo.objects.raw('select * from userinfo', using="default") ################### 原生SQL ################### from django.db import connection, connections cursor = connection.cursor() # cursor = connections['default'].cursor() cursor.execute("""SELECT * from auth_user where id = %s""", [1]) row = cursor.fetchone() # fetchall()/fetchmany(..) def values(self, *fields): # 获取每行数据为字典格式 def values_list(self, *fields, **kwargs): # 获取每行数据为元祖 def dates(self, field_name, kind, order='ASC'): # 根据时间进行某一部分进行去重查找并截取指定内容 # kind只能是:"year"(年), "month"(年-月), "day"(年-月-日) # order只能是:"ASC" "DESC" # 并获取转换后的时间 - year : 年-01-01 - month: 年-月-01 - day : 年-月-日 models.DatePlus.objects.dates('ctime','day','DESC') def datetimes(self, field_name, kind, order='ASC', tzinfo=None): # 根据时间进行某一部分进行去重查找并截取指定内容,将时间转换为指定时区时间 # kind只能是 "year", "month", "day", "hour", "minute", "second" # order只能是:"ASC" "DESC" # tzinfo时区对象 models.DDD.objects.datetimes('ctime','hour',tzinfo=pytz.UTC) models.DDD.objects.datetimes('ctime','hour',tzinfo=pytz.timezone('Asia/Shanghai')) """ pip3 install pytz import pytz pytz.all_timezones pytz.timezone(‘Asia/Shanghai’) """ def none(self): # 空QuerySet对象 #################################### # METHODS THAT DO DATABASE QUERIES # #################################### def aggregate(self, *args, **kwargs): # 聚合函数,获取字典类型聚合结果 from django.db.models import Count, Avg, Max, Min, Sum result = models.UserInfo.objects.aggregate(k=Count('u_id', distinct=True), n=Count('nid')) ===> {'k': 3, 'n': 4} def count(self): # 获取个数 def get(self, *args, **kwargs): # 获取单个对象 def create(self, **kwargs): # 创建对象 def bulk_create(self, objs, batch_size=None): # 批量插入 # batch_size表示一次插入的个数 objs = [ models.DDD(name='r11'), models.DDD(name='r22') ] models.DDD.objects.bulk_create(objs, 10) def get_or_create(self, defaults=None, **kwargs): # 如果存在,则获取,否则,创建 # defaults 指定创建时,其他字段的值 obj, created = models.UserInfo.objects.get_or_create(username='root1', defaults={'email': '1111111','u_id': 2, 't_id': 2}) def update_or_create(self, defaults=None, **kwargs): # 如果存在,则更新,否则,创建 # defaults 指定创建时或更新时的其他字段 obj, created = models.UserInfo.objects.update_or_create(username='root1', defaults={'email': '1111111','u_id': 2, 't_id': 1}) def first(self): # 获取第一个 def last(self): # 获取最后一个 def in_bulk(self, id_list=None): # 根据主键ID进行查找 id_list = [11,21,31] models.DDD.objects.in_bulk(id_list) def delete(self): # 删除 def update(self, **kwargs): # 更新 def exists(self): # 是否有结果
Django终端打印SQL语句
在Django项目的settings.py文件中,在最后复制粘贴如下代码:
LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'handlers': { 'console':{ 'level':'DEBUG', 'class':'logging.StreamHandler', }, }, 'loggers': { 'django.db.backends': { 'handlers': ['console'], 'propagate': True, 'level':'DEBUG', }, } }
即为你的Django项目配置上一个名为django.db.backends的logger实例即可查看翻译后的SQL语句。
在Python脚本中调用Django环境
import os if __name__ == '__main__': os.environ.setdefault("DJANGO_SETTINGS_MODULE", "项目名字.settings") import django django.setup() from app01 import models books = models.Book.objects.all() print(books)
# 书 class Book(models.Model): title = models.CharField(max_length=32) publish_date = models.DateField(auto_now_add=True) price = models.DecimalField(max_digits=5, decimal_places=2) memo = models.TextField(null=True) # 创建外键,关联publish publisher = models.ForeignKey(to="Publisher") # 创建多对多关联author author = models.ManyToManyField(to="Author") def __str__(self): return "<Book object: {} {}>".format(self.id, self.title) # 出版社 class Publisher(models.Model): name = models.CharField(max_length=32) city = models.CharField(max_length=32) def __str__(self): return "<Publisher object: {} {}>".format(self.id, self.name) # 作者 class Author(models.Model): name = models.CharField(max_length=32) age = models.IntegerField() phone = models.CharField(max_length=11) def __str__(self): return "<Author object: {} {}>".format(self.id, self.name)
""" 查找所有书名里包含金老板的书 查找出版日期是2018年的书 查找出版日期是2017年的书名 查找价格大于10元的书 查找价格大于10元的书名和价格 查找memo字段是空的书 查找在北京的出版社 查找名字以沙河开头的出版社 查找“沙河出版社”出版的所有书籍 查找每个出版社出版价格最高的书籍价格 查找每个出版社的书名以及出的书籍数量 查找作者名字里面带“小”字的作者 查找年龄大于30岁的作者 查找手机号是155开头的作者 查找手机号是155开头的作者的姓名和年龄 查找每个作者写的价格最高的书籍价格 查找每个作者的姓名以及出的书籍数量 查找书名是“跟金老板学开车”的书的出版社 查找书名是“跟金老板学开车”的书的出版社所在的城市 查找书名是“跟金老板学开车”的书的出版社的名称 查找书名是“跟金老板学开车”的书的出版社出版的其他书籍的名字和价格 查找书名是“跟金老板学开车”的书的所有作者 查找书名是“跟金老板学开车”的书的作者的年龄 查找书名是“跟金老板学开车”的书的作者的手机号码 查找书名是“跟金老板学开车”的书的作者们的姓名以及出版的所有书籍名称和价钱 """
答案
import os if __name__ == "__main__": os.environ.setdefault("DJANGO_SETTINGS_MODULE", "day_73_test.settings") import django django.setup() from app01 import models from django.db.models import Avg, Sum, Max, Min, Count,Q,F # 查找所有书名里包含金老板的书 ret=models.Book.objects.filter(title__contains="金老板") # 查找出版日期是2018年的书 ret=models.Book.objects.filter(publish_date__year='2018') # 查找出版日期是2017年的书名 ret=models.Book.objects.filter(publish_date__year='2017').values_list('title') # <QuerySet [('跟金老板学开潜艇',)]> ret=models.Book.objects.filter(publish_date__year='2017').values('title') # <QuerySet [{'title': '跟金老板学开潜艇'}]> # 查找价格大于10元的书 ret=models.Book.objects.filter(price__gt=10) # 查找价格大于10元的书名和价格 ret=models.Book.objects.filter(price__gt=10).values('title','price') # 查找memo字段是空的书 ret=models.Book.objects.filter(memo__isnull=True) ret=models.Book.objects.filter(Q(memo='')|Q(memo__isnull=True)) # 查找在北京的出版社 ret=models.Publisher.objects.filter(city='北京') ret=models.Publisher.objects.filter(city__contains='北京') # 查找名字以沙河开头的出版社 ret=models.Publisher.objects.filter(name__istartswith='沙河') # 查找“沙河出版社”出版的所有书籍 ret=models.Book.objects.filter(publisher__name='沙河出版社') # 正向查询 # 查找每个出版社出版价格最高的书籍价格 ret=models.Publisher.objects.annotate(max_price=Max('book__price')).values('name','max_price') ret=models.Book.objects.values('publisher__name').annotate(max_price=Max('price')).values('publisher_id','max_price','title') # publisher_id 直接取得字段 print(ret) # 只要是两个 model 类通过 ForeignKey 或者 ManyToMany 关联起来,那么就可以使用 annotate 方法来统计数量 # 查找每个出版社的名字以及出的书籍数量 ret=models.Publisher.objects.all().annotate(book_num=Count('book')).values() # 查找作者名字里面带“小”字的作者 ret=models.Author.objects.filter(name__contains='小') # 查找年龄大于30岁的作者 ret=models.Author.objects.filter(age__gt=30) # 查找手机号是155开头的作者 ret=models.Author.objects.filter(phone__istartswith='155') # 查找手机号是155开头的作者的姓名和年龄 ret = models.Author.objects.filter(phone__istartswith='155').values('name','age') # values()和for循环拿到值的效果一样 # 查找每个作者写的价格最高的书籍价格 ret=author_list=models.Author.objects.all().annotate(book_price=Max('book__price')).values('book__price') # for obj in author_list: # print(obj.book_price) # 查找每个作者的姓名以及出的书籍数量 ? ret=models.Author.objects.annotate(book_num=Count('book__id')).values('name','book_num') # 查找书名是“跟金老板学开车”的书的出版社 ret=models.Publisher.objects.filter(book__title='跟金老板学开车') # 查找书名是“跟金老板学开车”的书的出版社所在的城市 ret=models.Publisher.objects.filter(book__title='跟金老板学开车').values('city') # 查找书名是“跟金老板学开车”的书的出版社的名称 ret=models.Publisher.objects.filter(book__title='跟金老板学开车').values_list('name') # ret=models.Publisher.objects.filter(book__title='跟金老板学开车').name # 报错 'QuerySet' object has no attribute 'name' # 查找书名是“跟金老板学开车”的书的出版社出版的其他书籍的名字和价格 ret = models.Publisher.objects.get(book__title='跟金老板学开车') # <Publisher object:1 沙河出版社> <class 'app01.models.Publisher'> ret=models.Book.objects.filter(publisher= models.Publisher.objects.get(book__title='跟金老板学开车')).exclude( title='跟金老板学开车').values('title','price') ret=models.Publisher.objects.get(book__title='跟金老板学开车') ret=models.Publisher.objects.get(book__title='跟金老板学开车').book_set # <locals>.RelatedManager'> 管理对象,反向查询 ret=models.Publisher.objects.get(book__title='跟金老板学开车').book_set.exclude(title='跟金老板学开车').values('title','price') # 查找书名是“跟金老板学开车”的书的所有作者 ret=models.Book.objects.filter(title='跟金老板学开车').values('author__name') # 查找书名是“跟金老板学开车”的书的作者的年龄 ret=models.Book.objects.filter(title='跟金老板学开车').values('author__age') # 查找书名是“跟金老板学开车”的书的作者的手机号码 ret = models.Book.objects.filter(title='跟金老板学开车').values('author__phone') # 查找书名是“跟金老板学开车”的书的作者们的姓名以及出版的所有书籍名称和价钱 ret=models.Author.objects.filter(book__title='跟金老板学开车').values('book__title','book__price').distinct() # 去重 ret=models.Book.objects.get(title='跟金老板学开车').author # .<locals>.ManyRelatedManager'> # 正向查询 ret=models.Book.objects.get(title='跟金老板学开车').author.values('book__title','book__price').distinct() print(ret,type(ret))
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix