ORM Lens

DOL004 — Prefer .exists() over .first() is not None

Default severity: info · Applicability: safe · Category: queryset

Detects qs.first() is not None used as an existence test. Like DOL003, this fetches and hydrates an entire row only to discard it after a None comparison; .exists() issues SELECT 1 ... LIMIT 1 and transfers nothing but the answer. Keep .first() only when you go on to use the returned instance. The QuickFix (“Rewrite to .exists()”) is semantically identical and safe to auto-apply.

Bad

if Invite.objects.filter(email=email).first() is not None:
    raise AlreadyInvited

Good

if Invite.objects.filter(email=email).exists():
    raise AlreadyInvited

Suppress

# orm-lens-disable-next-line DOL004

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