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

相关推荐
西西弗Sisyphus10 分钟前
全面掌握Python时间处理
python·time
java1234_小锋2 小时前
一周学会Flask3 Python Web开发-http响应状态码
python·flask·flask3
奔跑吧邓邓子3 小时前
【Python爬虫(12)】正则表达式:Python爬虫的进阶利刃
爬虫·python·正则表达式·进阶·高级
码界筑梦坊4 小时前
基于Flask的京东商品信息可视化分析系统的设计与实现
大数据·python·信息可视化·flask·毕业设计
pianmian14 小时前
python绘图之箱型图
python·信息可视化·数据分析
csbDD4 小时前
2025年网络安全(黑客技术)三个月自学手册
linux·网络·python·安全·web安全
赔罪5 小时前
Python 高级特性-切片
开发语言·python
伊一大数据&人工智能学习日志6 小时前
selenium爬取苏宁易购平台某产品的评论
爬虫·python·selenium·测试工具·网络爬虫
说是用户昵称已存在6 小时前
Pycharm+CodeGPT+Ollama+Deepseek
ide·python·ai·pycharm
Fansv5876 小时前
深度学习-2.机械学习基础
人工智能·经验分享·python·深度学习·算法·机器学习