About Ternary Operators

Python ternary operators not only don't return the value under false conditions but also don't execute it, for example, division by zero will be ignored in the following snippet, because the condition is falsey:

1
2
>>> 100 / 0 if False else "OK"
"OK"

You can safely use this fact for Django cases like this:

1
name = post.author.get_full_name() if post.author else "Anonymous"

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