当前位置: 首页 > 后端技术 > Python

Django笔记24:数据库函数的比较和转换函数

时间:2023-03-25 20:38:06 Python

这篇笔记开始介绍几个数据库函数。下面是几个函数及其作用。Cast转换类型Coalesce优先考虑值Greatest并返回更大的值。Nullif值相同ReturnNone1,model准备这篇笔记,我们主要以Author和Entry模型为例。以下是Author模型:classAuthor(models.Model):name=models.CharField(max_length=200)email=models.EmailField(null=True,default=None)age=models.IntegerField(null=True,blank=True)alias=models.CharField(max_length=50,null=True,blank=True)goes_by=models.CharField(max_length=50,null=True,blank=True)一般来说,对于CharField字段,我们不推荐允许null=True存在,因为在这种情况下,数据库中会有两个null值,一个是null,一个是空字符串''。这里允许这样操作,是为了方便介绍后面的功能。注意,数据库相关的函数都在django.db.models.functions模块下2.Cast转换类型Cast,我们可以理解为一种转换数据类型,比如Author中,age字段就是一个Integer数据。但是如果我们想在获取数据的时候直接将数据转换成浮点型数据,可以使用Cast()函数,通过output_field=FloatField()参数指定输出类型。#先创建数据fromblog.modelsimportAuthorAuthor.objects.create(name='hunter',age=25)返回一个新字段,并通过Cast()函数指定输出类型:fromdjango.db.modelsimportFloatFieldfrom姜戈。db.models.functionsimportCastauthor=Author.objects.annotate(float_age=Cast('age',output_field=FloatField())).get(id=1)print(author.float_age)最后输出的是浮点型数据。3.Coalesce值优先。Coalesce这个词的意思是合并、联合,但是这里的函数表现出来的意思是先取值。Coalesce()接受多个字段或表达式作为参数,至少两个字段名,然后返回第一个非空字段的值(注意:空字符串''不被认为是空值)对于每个元素都必须是类型相同,否则会报错。对于Author模型,我们希望按照alias、goes_by和name字段的顺序取值。也就是说如果有alias字段就取alias的内容,否则就取goes_by的字段值,没有goes_by就取name字段。这种情况下可以使用Coalesce()来操作。先创建几条数据:Author.objects.create(alias="alias-1",goes_by='goes-by-1',name='name-1')Author.objects.create(goes_by='goes-by-2',name='name-2')Author.objects.create(name='name-3')Author.objects.create(alias="",goes_by='goes-by-4',名称='name-4')以上三条数据在数据库中的id分别为2、3、4、5。接下来,您可以测试Coalesce()函数fromdjango.db.models.functionsimportCoalesceauthor=Author.objects.annotate(new_field=Coalesce('alias','goes_by','name')).get(id=2)print(author.new_field)#outputalias-1author=Author.objects.annotate(new_field=Coalesce('alias','goes_by','name')).get(id=3)print(author.new_field)#输出goes-by-2author=Author.objects.annotate(new_field=Coalesce('alias','goes_by','name')).get(id=4)print(author.new_field)#outputname-3author=Author.objects.annotate(new_field=Coalesce('alias','goes_by','name')).get(id=5)print(author.new_field)#Outputanemptystring''在上面的例子中,我们测试了新字段的值优先级,以及本函数中空字符串和null的区别(该值会跳过空数据,但会取空字符串的字段值)。这里其实用的是空值的默认值,我们可以找到这个函数的另一种用法,就是用空值代替默认值。假设我们有一个字段,我们想在取值的时候实现它,如果该字段为null,那么我们想在取值的时候将其替换为另一个默认值,而不是返回null或者内存中的后续操作来替换默认值值,你可以这样做:fromdjango.db.modelsimportValueauthor=Author.objects.annotate(new_field=Coalesce('email',Value('xxx'))).get(id=5)print(author.new_field)#id等于5的Author数据,email字段为空,所以new_field的值替换成默认值'xxx',也可以用在聚合中,比如聚合Sum()时,如果没有满足条件的数据,聚合结果将为空,但我们可以自动将其更改为0:fromdjango.db.modelsimportSum,ValueAuthor.objects.aggregate(age_sum=Coalesce(Sum('age'),值(0)))4。Greatest返回最大值。Greatest()的用法与Coalesce相同。它接受两个或多个相同类型的元素并返回最大的一个。可以比较数字和时间等字段类型。在这个例子中,我们使用了Entry模型,我们只使用了两个整型字段:importEntryfromdjango.db.models.functionsimportGreatestEntry.objects.annotate(max_value=Greatest("number_of_comments","number_of_pingbacks")).get(id=2).max_value#max_value字段的值会取最大的number_of_comments和number_of_pingbacks这里我们还可以挖出一个show操作,也就是数值的下限。比如这两个字段的值没有达到我们想要的阈值,比如2。我们希望返回的值至少是2。你可以这样设计程序:Entry.objects.annotate(max_value=Greatest("number_of_comments","number_of_pingbacks",Value(2))).get(id=2).max_value注意:在MySQL和Oracle中,如果有字段值为Greatestnull,那么结果将返回null。必须注意的是,Least()取最小值,与Greatest相反,但用法相同。我就不多介绍了。5.如果Nullif值相同,则返回None得到两个字段也可以是表达式的结果,也可以是Value()的值,但两者的数据类型必须相同,用于判断两者的值是否相同。如果两个值相同,在Python中返回None,否则返回第一个表达式的值。示例用法如下:Entry.objects.annotate(new_field=NullIf("number_of_comments","number_of_pingbacks")).get(id=1).new_fieldEntry.objects.annotate(new_field=NullIf("number_of_comments",Value(2))).get(id=1).new_field以上就是本篇笔记的全部内容,下篇将介绍数据库函数date函数