About ORing Conditions

When checking for one of the multiple conditions with or, put the most straightforward and fastest conditions at the beginning, for example:

1
2
if user.is_superuser or user.groups.filter(name="Admins").exists():
    send_special_email(user)

When you are or-ing the conditions, Python looks for the first True-ish condition and ignores the rest. So by following this practice you can avoid unnecessary database queries or calculations.

Tips and Tricks Programming Development Performance Django 4.2 Django 3.2 Django 2.2 Python 3