ORM Lens

DOL013 — ForeignKey or OneToOneField without on_delete

Default severity: error · Applicability: suggestion · Category: model

Detects a ForeignKey(...) or OneToOneField(...) call with no on_delete= keyword. ManyToManyField has no on_delete and is not checked. on_delete is required since Django 2.0 — omitting it raises TypeError the moment the model module loads, so this is caught at author-time before you ever run the app. The QuickFix (“Add on_delete=models.CASCADE (edit to your policy)”) inserts on_delete=models.CASCADE as a template. It is a suggestion, not safe: the deletion policy is a real design decision — CASCADE silently deletes dependents, while PROTECT, SET_NULL, SET_DEFAULT, or DO_NOTHING may be what the data actually needs.

Bad

class Book(models.Model):
    author = models.ForeignKey(Author)

class Profile(models.Model):
    user = models.OneToOneField(User)

Good

class Book(models.Model):
    author = models.ForeignKey(Author, on_delete=models.CASCADE)

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)

Suppress

# orm-lens-disable-next-line DOL013

Or per-workspace in .vscode/settings.json: {"djangoOrmLens.rules": {"DOL013": "off"}}.