0Pricing
Django Academy · 课时

字段选项:null、blank、default

控制列可以接受哪些值

字段选项:null、blank、default 是 CoddyKit 上的免费 Django Academy 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Django Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Django Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Tune Each Field

Beyond the type, every field accepts options that control what values it will accept and how it behaves.

null Is About the Database

The null option decides whether the database column may store NULL. With null=True, the column can be empty in the database.

summary = models.TextField(null=True)

blank Is About Forms

The blank option controls validation, not the database. With blank=True, forms allow the field to be left empty.

summary = models.TextField(blank=True)

null and blank Are Different

Remember the split: null affects the database layer, while blank affects form validation. They solve two separate problems.

Avoid null on Text Fields

For string fields, skip null and just use blank=True. Django prefers an empty string over NULL to keep two empty states from existing.

note = models.CharField(max_length=100, blank=True)

default Supplies a Value

The default option sets the value used when none is given, so new rows start with sensible data.

views = models.IntegerField(default=0)

Callable Defaults

A default can be a callable like timezone.now. Pass the function itself, with no parentheses, so it runs for each new row.

created = models.DateTimeField(default=timezone.now)

unique Prevents Duplicates

Add unique=True and the database refuses two rows with the same value, perfect for usernames or slugs. 🚫

username = models.CharField(max_length=50, unique=True)

choices Limits the Options

The choices option restricts a field to a fixed set of values and renders a dropdown in forms and the admin.

STATUS = [("d", "Draft"), ("p", "Published")]
status = models.CharField(max_length=1, choices=STATUS)

Options Stack Together

You can combine options freely on one field, like blank=True with default. Stack them to express exactly what you need.

tagline = models.CharField(max_length=120, blank=True, default="")

Changes Need a Migration

Editing an option changes the schema. After tweaking null, default, or unique, run a migration so the database matches.

Quick Check

What is the real difference between null and blank?

Recap: Field Options

You now know that null is database-level, blank is form-level, default seeds values, and unique blocks duplicates. Tune fields with confidence! ✨

常见问题解答

「字段选项:null、blank、default」课时是免费的吗?

是的 — 「字段选项:null、blank、default」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Django Academy 课程的其余内容,请升级到 CoddyKit PRO。 Django Academy 课程共包含 4 节课。

「字段选项:null、blank、default」这节课中我会学到什么?

控制列可以接受哪些值 你通过在浏览器中直接运行的动手代码来练习 Django Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Django Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Django Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「字段选项:null、blank、default」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Django Academy 课中编写并运行代码吗?

能。每节 Django Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 定义模型类
  2. 选择字段类型
  3. 字段选项:null、blank、default
  4. str 与 Meta 类
← 返回 Django Academy