经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Django » 查看文章
Django笔记二十三之case、when操作条件表达式搜索、更新等操作
来源:cnblogs  作者:XHunter  时间:2023/4/14 9:19:31  对本文有异议

本文首发于公众号:Hunter后端
原文链接:Django笔记二十三之条件表达式搜索、更新等操作

这一篇笔记将介绍条件表达式,就是如何在 model 的使用中根据不同的条件筛选数据返回。

这个操作类似于数据库中 if elif else 的逻辑。

以下是本篇笔记的目录:

  1. model 和数据准备
  2. When 和 Case 操作新增字段返回
  3. 条件搜索
  4. 条件更新
  5. 条件聚合

1、model 和数据准备

这篇笔记我们用到的 model 是 Client,放在 blog/models.py 下

以下是 Client 的 model 定义:

  1. class Client(models.Model):
  2. REGULAR = 'R'
  3. GOLD = 'G'
  4. PLATINUM = 'P'
  5. ACCOUNT_TYPE_CHOICES = [
  6. (REGULAR, 'Regular'),
  7. (GOLD, 'Gold'),
  8. (PLATINUM, 'Platinum'),
  9. ]
  10. name = models.CharField(max_length=50)
  11. registered_on = models.DateField()
  12. account_type = models.CharField(
  13. max_length=1,
  14. choices=ACCOUNT_TYPE_CHOICES,
  15. default=REGULAR,
  16. )

其中 choices 的操作在前面字段类型中都有介绍到,这里不再赘述。

然后 migrate 相关操作这里不多说了,接下来插入一些数据,在 shell 中执行:

  1. from blog.models import Client
  2. Client.objects.create(name="client_1", registered_on="2020-01-01", account_type="R")
  3. Client.objects.create(name="client_2", registered_on="2021-07-12", account_type="G")
  4. Client.objects.create(name="client_3", registered_on="2022-09-20", account_type="P")
  5. Client.objects.create(name="client_4", registered_on="2022-12-07", account_type="P")

接下来介绍我们操作的知识点。

2、When 和 Case 操作新增字段返回

我们使用的条件表达式使用的 When 和 Case 的函数,这个其实就对应于 SQL 里的 CASE 和 WHEN 函数。

我们先来说一下需求,我们在获取 Client 数据的时候,想要知道这条数据 registered_on 日期字段所在的季节,比如 1月就是 Spring,7月就是 Autumn。

怎么处理呢?

很简单,先获取 Client 数据,然后根据 registered_on 字段判断月份,比如在 1,2,3 之间就是 Spring。

这种方法是可行的,但是如果我们有另一个需求,比如说想要筛选出所有季节为 Spring 的数据呢

(这个例子其实不太恰当,因为这种操作,我们可以直接通过 filter(registered_on__month__in=[1,2,3])来筛选,但是这里我们强行要求使用filter(季节='Spring')的这种形式来操作)

那么这时候就可以用上我们的 When Case 的用法了。

在下面的操作中,我们通过判断 registered_on 字段的月份区间来得到一个新的字段:

  1. from django.db.models import Case, Value, When
  2. from blog.models import Client
  3. results = Client.objects.annotate(
  4. season=
  5. Case(
  6. When(registered_on__month__in=[1,2,3], then=Value("Spring")),
  7. When(registered_on__month__in=[4,5,6], then=Value("Summer")),
  8. When(registered_on__month__in=[7,8,9], then=Value("Autumn")),
  9. When(registered_on__month__in=[10,11,12], then=Value("Winter")),
  10. default=Value("Spring")
  11. )
  12. )

在上面的代码中,我们通过 annotate() 来新建一个 season 字段,这个字段的值是根据 registered_on 的月份所在区间来为 season 赋值。

Case() 函数内包含了四种 When 的可能性,然后会有一个 default 默认值

在每一个 When() 函数里,前一个是个表达式,可以是这种形式,也可以是 Q() 操作的语句,then= 表示如果满足前面的表达式,那么值的内容将会是后面的值。

在值的定义里,我们这里用到了 Value() 函数,Value() 表示其值是一个字符串。

获取字段值

如果该字段取值是获取某个字段的内容,比如 Client 里的 name 字段,就不需要 Value() 函数来操作,可以直接使用:

  1. When(registered_on__month__in=[1,2,3], then="name")

或者通过 F() 函数来取字段值:

  1. from django.db.models import F
  2. When(registered_on__month__in=[1,2,3], then=F("name"))

在不需要对字段内容进行操作的情况下,上面两条命令的作用是一样的

3、条件搜索

还是前面的需求,我们需要对 Client 的数据进行数据筛选,筛选出 season 为 Spring 的数据,可以在上面的操作中接着 filter():

  1. results = Client.objects.annotate(
  2. season=
  3. Case(
  4. When(registered_on__month__in=[1,2,3], then=Value("Spring")),
  5. When(registered_on__month__in=[4,5,6], then=Value("Summer")),
  6. When(registered_on__month__in=[7,8,9], then=Value("Autumn")),
  7. When(registered_on__month__in=[10,11,12], then=Value("Winter")),
  8. default=Value("Spring")
  9. )
  10. ).filter(season="Spring")

根据条件进行filter

对于 Client 这个 model,我们想实现这样的搜索条件:

如果 account_type 的值为 Client.GOLD,则 registered_on 字段搜索一个月以前的数据

如果 account_type 的值为 Client.PLATINUM,则 registered_on 字段搜索一年前的数据

对于这个需求,在之前我们怎么做?
使用 Q() 语法来连接:

  1. from blog.models import Client
  2. from datetime import date, timedelta
  3. from django.db.models import Q
  4. a_month_ago = date.today() - timedelta(days=30)
  5. a_year_ago = date.today() - timedelta(days=365)
  6. condition = (Q(account_type=Client.GOLD) & Q(registered_on__lte=a_month_ago))| (Q(account_type= Client.PLATINUM) & Q(registered_on__lte= a_year_ago))
  7. Client.objects.filter(condition)

在这里,如果用到我们的 Case 和 When 的函数也是可以的:

  1. Client.objects.filter(
  2. registered_on__lte=Case(
  3. When(account_type=Client.GOLD, then=a_month_ago),
  4. When(account_type=Client.PLATINUM, then=a_year_ago)
  5. )
  6. )

一个例子

之前我在工作中遇到这样一种需求,假设有一个 TestModel,有一个 field_1 的字段,他的值被有 A, B, C 三种或者还有其他的值,但其他的值我们不关心

现在需要将数据按照 B,C,A 的顺序返回结果,那么这里用到 Case 和 When 的处理方法就可以,我们可以通过条件得出一个新的字段 priority,然后 order_by("priority") 即可

处理如下:

  1. TestModel.objects.annotate(
  2. priority=Case(
  3. When(field_1="B", then=1),
  4. When(field_1="C", then=2),
  5. When(field_1="A", then=3),
  6. default=4
  7. )
  8. ).order_by("priority")

4、条件更新

除了前面对数据进行条件的筛选,我们还可以根据条件来对数据进行更新

假设现在需求是对 registered_on 字段的年份进行条件更新:
年份为 2020年的 account_type 字段内容变为 Client.PLATINUM
年份为 2021年的 account_type 字段内容变为 Client.REGULAR

那么相应的代码应该如下:

  1. Client.objects.update(
  2. account_type=Case(
  3. When(registered_on__year=2020, then=Value(Client.PLATINUM)),
  4. When(registered_on__year=2021, then=Value(Client.REGULAR)),
  5. default=Value(Client.GOLD)
  6. )
  7. )

需要注意的是,在上面的代码中我们没有针对数据进行 filter() 操作,所以作用的是全表数据,其他非 2020 和 2021 年份的数据也会被更新,如果仅希望操作 2020 和 2021 年的数据,可以加上 filter() 的条件限制:

  1. Client.objects.filter(registered_on__year__in=[2020, 2021]).update(
  2. account_type=Case(
  3. When(registered_on__year=2020, then=Value(Client.PLATINUM)),
  4. When(registered_on__year=2021, then=Value(Client.REGULAR)),
  5. default=Value(Client.GOLD)
  6. )
  7. )

5、条件聚合

我们现在需要对数据根据条件进行聚合操作,比如 Client 这个 model,我们对其按照 account_type 分组,获取各自的总数。

代码如下:

  1. from django.db.models import Count, Q
  2. Client.objects.aggregate(
  3. regular=Count('pk', filter=(Q(account_type=Client.REGULAR))),
  4. gold=Count('pk', filter=Q(account_type=Client.GOLD)),
  5. platinum=Count('pk', filter=Q(account_type=Client.PLATINUM)),
  6. )

返回的结果为:

  1. {'regular': 1, 'gold': 0, 'platinum': 3}

这个操作对应于 MySQL 中的语句如下:

  1. select count(CASE WHEN account_type='R' THEN id ELSE null end) as regular,
  2. count(CASE WHEN account_type='G' THEN id ELSE null end) as gold,
  3. count(CASE WHEN account_type='P' THEN id ELSE null end) as platinum
  4. FROM blog_client;

我们也可以根据另一种方式来获取各自的总数数据,但是返回的结构是不一样的:

  1. Client.objects.values("account_type").annotate(count=Count("account_type"))

返回的结果形式为:

  1. <QuerySet [{'account_type': 'P', 'count': 3}, {'account_type': 'R', 'count': 1}]>

以上就是本篇笔记关于条件表达式的全部内容,在接下来几篇笔记中将会介绍 model 的数据库函数,大致的内容会是比较和转换函数、日期函数、数据公式、文本函数等。

如果想获取更多后端相关文章,可扫码关注阅读:
image

原文链接:https://www.cnblogs.com/hunterxiong/p/17316360.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号