《Django 5 By Example》阅读笔记:p651-p678

《Django 5 By Example》学习第9天,p651-p678总结,总计28页。

一、技术总结

1.aggregate()

(1)aggregate:ad-("to") + gregare("to collection into a flock(群), to gather")

因为ad 后面跟的是gregate,为了发音方便,ad演变为了ag。aggregate的本意是:vt. to combine into a single group(聚合, multiple things > single group)。

示例:The butterflies aggregate in dense groups(蝴蝶聚集在一起).

(2)数据库里面的annotate

aggregate()在数据库里面的含义是:aggregate refer to the process of summarizing or combining data from multiple row into a single result(multiple row > single result)。例如:SUM(),AVG(),COUNT(),MAX(),MIN()都称为聚合函数。

示例:

复制代码
SELECT AVG(price) FROM book;

(3) Django里面的aggregate

示例:

复制代码
# Average price across all books, provide default to be returned instead
# of None if no books exist.
>>> from django.db.models import Avg
>>> Book.objects.aggregate(Avg("price", default=0))
{'price__avg': 34.35}

2.annotate()

(1)annotate:ad-("to") + notare("to mark, to note")

因为ad 后面跟的是notare,为了发音方便,ad演变为了an。annotate的本意是:to add marks or notes to explain on sth(给.....作注解,给......加评注)。

(2)数据库里面的annotate

annotate()在数据库里面的含义是:annotate refers to adding supplementary information to queries or request.This can help provide context, make data more understandable, or enhance the the functionality of queries or results.

示例:

复制代码
COMMENT ON COLUMN employees.salary IS 'Employee annual salary';

(3)Django里面的annotate

Django里面的annotate()函数的作用是:Annotates each object in the QuerySet with the provided list of query expressions(使用已提供的query expressions 对 QuerySet 中的每个对象进行注解。添加注解的结果是QuerySet中的每个objec会多一个字段)。

示例:

复制代码
# Build an annotated queryset
>>> from django.db.models import Count
>>> q = Book.objects.annotate(Count("authors"))
# Interrogate the first object in the queryset
>>> q[0]
<Book: The Definitive Guide to Django>
>>> q[0].authors__count
2
# Interrogate the second object in the queryset
>>> q[1]
<Book: Practical Django Projects>
>>> q[1].authors__count
1

3.DRF serializer

(1)定义

p648, Serializer: Provides serialization for normal Python class instances。

(2)分类

Serializer又细分为Serializer, ModelSerializer, HyperlinkedModelSerializer.

(3)Field

IntegerField(),SerializerMethodField(),StringRelatedField()。

(4)nested serializer

nested serializer 可用于替代 StringRelatedField()。

4.DRF 分页

DRF使用PageNumberPagination 进行分页。

5.DRF authentication

(1)BasicAuthentication

(2)TokenAuthentication

(3)SessionAuthentication

(4)RemoteUserAuthentication

又提到了认证,我快要晕了,差不多每个项目都搞一次认证,能抽象出来做一个整体的介绍不?毕竟认证这块在实际项目中并不会变动那么大,真没必要花那么多章节来介绍认证的。

二、英语总结(生词:0)

无。

三、其它

(1)json_pp

复制代码
curl http://127.0.0.1:8000/api/subjects/ | json_pp

不知道为什么,感觉国外的的作者特别喜欢使用curl,本人上次看到还是curl结合jq使用,这次是curl结合json_pp使用,在postman和命令行之间反复跳。可能是postman输入地址太麻烦?

重新梳理了下aggregate()、annotate()等函数的含义及用法,之前学得太糙了,不够细,慢慢的就忘掉了它的用法。

chapter 15简评:以在线学习平台为例,介绍了如何在Django项目中集成django-rest-framework。这个学习平台的介绍又是在前面几章,感觉排版很不合理,如果都是一个项目的话最好还是用一整个章节来介绍比较好。同时既然都使用了django-rest-framework了,咋不整一个前端框架呢?

四、参考资料

1. 编程

(1) Antonio Melé,《Django 5 By Example》:https://book.douban.com/subject/37007362/

2. 英语

(1) Etymology Dictionary:https://www.etymonline.com

(2) Cambridge Dictionary:https://dictionary.cambridge.org

欢迎搜索及关注:编程人(a_codists)

相关推荐
大连好光景5 分钟前
Python打日志
运维·python·运维开发
syt_biancheng10 分钟前
博客系统全流程测试总结
python·selenium·测试用例·压力测试·postman
可信计算29 分钟前
【算法随想】一种基于“视觉表征图”拓扑变化的NLP序列预测新范式
人工智能·笔记·python·算法·自然语言处理
张广涛35 分钟前
【无标题】
python
爱笑的眼睛1139 分钟前
超越剪枝与量化:下一代AI模型压缩工具的技术演进与实践
java·人工智能·python·ai
yaoh.wang39 分钟前
力扣(LeetCode) 14: 最长公共前缀 - 解法思路
python·程序人生·算法·leetcode·面试·职场和发展·跳槽
reasonsummer1 小时前
【办公类-18-07】20251215(Python)“口腔检查涂氟信息”批量生成打印(区名、学号、姓名、学校、班级、身份证、户籍、性别、民族)
开发语言·python
Rabbit_QL1 小时前
【PyTorch】detach:从计算图中切断梯度的原理与实践
人工智能·pytorch·python
晨曦5432101 小时前
MySQL与Python数学函数大比拼
python·mysql
一个无名的炼丹师1 小时前
【硬核实战】Python处理多源异构文档:从读取到智能信息提取的统一框架深度剖析
开发语言·python