《Django 5 By Example》閱讀筆記:p237-p338

codists發表於2024-11-25

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

相關文章