《Django 5 By Example》阅读笔记:p237-p338

《Django 5 By Example》学习第11天,p237-p338总结,总计102页。

一、技术总结

1.follow system(关注功能)

表之间的关系有三种:OneToOneField,many-to-one(使用Foreignkey()),ManyToManyField。有时候为了更好的描述对象之间的关系,需要多创建一张中间表:Creating an intermediate model is necessary when you want to store additional information on the relationship, for example, the date when the relationship was created, or a field that describes the nature of the relationship。

例如关注功能:

复制代码
class Contact(models.Model):

    user_from = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='rel_from_set')

    user_to = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='rel_to_set')

    created = models.DateTimeField(auto_now_add=True)



    class Meta:

        indexes = [

            models.Index(fields=['-created']),

         ]

        ordering = ['-created']



    def __str__(self):

        return f'{self.user_from.username} follows {self.user_to.name}'

2.monkey patch

p289, You use the add_to_class() method of Django models to monkey patch the User model。monkey patch的意思是:Monkey patch is a technique used to dynamically update the code at runtime without altering the source code。示例:

复制代码
# monkey patch 示例

user_model = get_user_model()

user_model.add_to_class(

    'following',

    models.ManyToManyField('self', through=Contact, symmetrical=False, related_name='followers')

)

注:尽量少用monkey patch。

3.activity stream(活动流)功能

4.contenttypes

p300, contenttypes 的作用:This application can track all models installed in your project and provides a generic interface to interact with your models.

5.denormalization(反规范化)

p315, Denormalization is making data redundant in such a way that it optimizes read performance. For example, you might be copying related data to an object to avoid expensive read queries to the database when retrieving the related data.

6.signal

7.django-debug-toolbar

用于完善debug功能,在界面通过toolbar显示一些统计信息。个人觉得在实际开发中可有可无。

8.使用redi做缓存

使用的 package 名称也叫redis。在实际业务也中经常使用,但是不复杂,掌握常用的方法即可。

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

1.thewart

p292. Usernames, unlike sequential IDs, thwart enumeration attacks by obscuring your data structure.

vt. to stop sth from happing.

2.every once in a while

p327, Redis stores everything in memory, but the data can be persisted by dumping the dataset to disk every once in a while, or by adding each command to a log.

idiom. sometimes, but not regularly. 也写作 every so often。

三、其它

chapter 07 简评:4-7 章是一个完整的项目(bookmarks),建议从第 4 章按顺序阅读到第 7 章,不要跳过某个章节,因为作者的代码是连续,跳过之后项目可能无法运行。

Pycharm(2024.3 Professional Edition) :Settings > Build, Execution, Deployment > Python Debugger新增了一个功能 "Run debugger in server mode",然后 Pycharm 默认是勾选的,导致我在 WSL 使用 debug 启动后一直无法访问。忍了那么久终于可以debug了,气 die! Pycharm,你再这样我明年就不续费了。

四、参考资料

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)

相关推荐
一乐小哥3 分钟前
一口气同步10年豆瓣记录———豆瓣书影音同步 Notion分享 🚀
后端·python
华研前沿标杆游学13 分钟前
华为在国内搞的研发基地有多野?标杆游学带你解锁“研发界顶流”
python
小胖墩有点瘦17 分钟前
【基于深度学习的中草药识别系统】
人工智能·python·深度学习·课程设计·计算机毕业设计·中草药识别
正在走向自律32 分钟前
Ubuntu系统下Python连接国产KingbaseES数据库实现增删改查
开发语言·数据库·python·ubuntu·kingbasees·ksycopg2
Calihen的学习日志1 小时前
【Pandas】3.1-数据预处理:列的基本操作
python·pandas
打螺丝否1 小时前
稠密矩阵和稀疏矩阵的对比
python·机器学习·矩阵
这里有鱼汤1 小时前
你以为 FastAPI 足够强?其实 Litestar 能让你的项目更轻量高效
后端·python
大学生毕业题目2 小时前
毕业项目推荐:83-基于yolov8/yolov5/yolo11的农作物杂草检测识别系统(Python+卷积神经网络)
人工智能·python·yolo·目标检测·cnn·pyqt·杂草识别
Kyln.Wu2 小时前
【python实用小脚本-205】[HR揭秘]手工党逐行查Bug的终结者|Python版代码质量“CT机”加速器(建议收藏)
开发语言·python·bug
计算机毕业设计木哥2 小时前
Python毕业设计推荐:基于Django的饮食计划推荐与交流分享平台 饮食健康系统 健康食谱计划系统
开发语言·hadoop·spring boot·后端·python·django·课程设计