Zum Inhalt springen

Count Vs Exists (ARCH2063)

Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.

Key python.orm.count-vs-exists · Severity info · Category orm

Using count() > 0 to test for presence counts all rows; exists() stops at the first row and gives the same answer far more cheaply.

if User.objects.filter(email=email).count() > 0:
raise ValueError("taken") # needless full count
if User.objects.filter(email=email).exists():
raise ValueError("taken") # short-circuits at first row

Use the short-circuiting exists() instead of count() > 0 for presence checks.