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

相关推荐
山沐与山8 分钟前
【设计模式】Python责任链模式:从入门到实战
python·设计模式·责任链模式
luoluoal12 分钟前
基于python的图像的信息隐藏技术研究(源码+文档)
python·mysql·django·毕业设计·源码
电商API_1800790524722 分钟前
淘宝商品视频提取API全解析:从授权到落地实战
爬虫·python·信息可视化·数据分析·音视频
计算机程序设计小李同学30 分钟前
基于 Flask 的养猪场信息模拟系统
后端·python·flask
Iridescent112140 分钟前
Iridescent:Day38
python
熬夜敲代码的小N40 分钟前
2026 职场生存白皮书:Gemini Pro 实战使用指南
人工智能·python·ai·职场和发展
Franklin1 小时前
移植一个Pycharm的复杂项目:
ide·python·pycharm
94621931zyn61 小时前
外观设置 - Cordova 与 OpenHarmony 混合开发实战
笔记·python
彼岸花开了吗1 小时前
构建AI智能体:六十五、模型智能训练控制:早停机制在深度学习中的应用解析
人工智能·python
BoBoZz191 小时前
3D 医学扫描同时显示患者的皮肤、骨骼的 3D 模型(通过等值面提取),以及三个正交切片
python·vtk·图形渲染·图形处理