ORM Lens

DOL022 — datetime.utcnow() is deprecated

Default severity: warning · Applicability: suggestion · Category: datetime

Detects bare datetime.utcnow() calls. utcnow() is deprecated since Python 3.12 and — despite the name — returns a naive datetime: the UTC wall-clock time with no tzinfo attached. Mixed with the aware datetimes a USE_TZ=True Django project produces, that yields the same RuntimeWarning on save and TypeError on comparison as DOL021, plus silent off-by-timezone bugs when the value is later interpreted as local time. In Django code use django.utils.timezone.now(); in framework-free stdlib code use datetime.now(timezone.utc). The QuickFix (“Replace with timezone.now()”) applies the Django form; add the import yourself.

Bad

from datetime import datetime

token.issued_at = datetime.utcnow()

Good

from django.utils import timezone

token.issued_at = timezone.now()

Suppress

# orm-lens-disable-next-line DOL022

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