Django 团队宣布发布 Django 4.2,此版本已被指定为长期支持 (LTS) 版本,这意味着至少在未来三年内支持最新的安全和数据丢失修复程序,还将在接下来的八个月(直到 2023 年 12 月)内收到崩溃错误、新引入功能中的主要功能错误以及旧版本 Django 的回归修复。
Django 4.2 的新特性:
Psycopg 3 支持
- Django 现在支持 psycopg 3.1.8 或更高版本。要更新您的代码,请安装 psycopg 库,无需更改 ENGINE,因为django.db.backends.postgresql支持这两个库。
- 建议及时更新,因为可能会弃用和删除对 psycopg2 的支持代码。
对列和表的评论功能
新的 Field.db_comment 和 Meta.db_table_comment 选项允许分别在列和表上创建注释。
from django.db import models
class Question(models.Model):
    text = models.TextField(db_comment=”Poll question”)
    pub_date = models.DateTimeField(
        db_comment=”Date and time when the question was published”,
    )
    class Meta:
        db_table_comment = “Poll questions”
class Answer(models.Model):
    question = models.ForeignKey(
        Question,
        on_delete=models.CASCADE,
        db_comment=”Reference to a question”,
    )
    answer = models.TextField(db_comment=”Question answer”)
    class Meta:
        db_table_comment = “Question answers” 
此外,新的 AlterModelTableComment 操作允许更改在 Meta.db_table_comment 中定义的表注释。
BREACH 攻击的缓解措施
- GZipMiddleware现在包括对 BREACH 攻击的缓解措施。它会将最多 100 个随机字节添加到 gzip 响应中,使 BREACH 攻击更加困难。
内存文件存储
- 新的 django.core.files.storage.InMemoryStorage类提供了一种非持久性存储,可通过避免磁盘访问来加快测试速度。
自定义文件存储
- 新的 STORAGES设置允许配置多个自定义文件存储后端。它还控制用于管理文件("default"键)和静态文件("staticfiles"键)的存储引擎。
- 旧的 DEFAULT_FILE_STORAGE和STATICFILES_STORAGE设置自本版本起已弃用。
其他次要功能可在更新公告中查看。
 
                            