《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)

相关推荐
3824278274 分钟前
Edge开发者工具:保留日志与禁用缓存详解
java·前端·javascript·python·selenium
nuowenyadelunwen5 分钟前
Harvard CS50 Week 6 Python
开发语言·python
饼干,6 分钟前
期末考试3
开发语言·人工智能·python
曲幽9 分钟前
FastAPI响应实战:从JSON到HTML,轻松驾驭多种数据格式
python·html·json·fastapi·web·jinja2·responses
jackylzh10 分钟前
数据集标签文件转换方法--将 XML 文件类型转化为 TXT 文件类型
人工智能·python·深度学习
linuxxx11013 分钟前
request.build_absolute_uri()为什么没有获得端口?
python·nginx·django
小北方城市网13 分钟前
第 5 课:后端工程化进阶 ——Python 分层架构 + 中间件 + 日志 / 异常统一处理(打造企业级高可用后端)
数据库·人工智能·python·mysql·数据库架构
山山而川 潺潺如镜13 分钟前
python防止程序多开,但程序运行脚本
android·开发语言·python
大千AI助手23 分钟前
HiveOperator 中 hql 模板路径解析失败的原因分析
hive·python·任务调度·airflow·模版·大千ai助手·hiveoperator
小北方城市网25 分钟前
第 4 课:前端工程化进阶 ——Vue 核心语法 + 组件化开发(前端能力质的飞跃)
大数据·开发语言·数据库·python·状态模式·数据库架构