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

相关推荐
独行soc17 分钟前
2025年渗透测试面试题总结-275(题目+回答)
网络·python·安全·web安全·网络安全·渗透测试·安全狮
番石榴AI2 小时前
java版的ocr推荐引擎——JiaJiaOCR 2.0重磅升级!纯Java CPU推理,新增手写OCR与表格识别
java·python·ocr
时光轻浅,半夏挽歌2 小时前
python不同格式文件的读写方式(json等)
python·json
测试人社区-千羽3 小时前
边缘计算场景下的智能测试挑战
人工智能·python·安全·开源·智能合约·边缘计算·分布式账本
抽象带篮子3 小时前
Pytorch Lightning 框架运行顺序
人工智能·pytorch·python
哇哈哈&3 小时前
安装wxWidgets3.2.0(编译高版本erlang的时候用,不如用rpm包),而且还需要高版本的gcc++19以上,已基本舍弃
linux·数据库·python
luod4 小时前
pymysql执行DDL语句
python
song5014 小时前
鸿蒙 Flutter 图像识别进阶:物体分类与花卉识别(含离线模型)
人工智能·分布式·python·flutter·3d·华为·分类
Mqh1807624 小时前
day 35 文件的拆分和使用
python
虚假程序设计5 小时前
pythonnet 调用C接口
c语言·python