# Django数据库完整性错误：NOT NULL约束失败：app_model.field

- **ID:** `python/django-model-save-error`
- **领域:** python
- **类别:** data_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

尝试保存模型实例时，必填字段设置为NULL。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.x | active | — | — |

## 解决方案

1. **Provide a default value or set the field before save** (95% 成功率)
   ```
   instance.field = value; instance.save()
   ```
2. **Allow NULL in model definition** (90% 成功率)
   ```
   field = models.CharField(null=True, blank=True)
   ```

## 无效尝试

- **Setting field to empty string** — Empty string is not NULL but may still violate constraints if blank=False. (70% 失败率)
- **Ignoring the field** — Field must have a value. (90% 失败率)
