From 60613ef5162eefbe82ea48f7de6a0c55f9cb8a4e Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Tue, 30 Aug 2022 06:22:33 +0200 Subject: [PATCH] Merged nested if statements in select_related_descend(). --- django/db/models/query_utils.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/django/db/models/query_utils.py b/django/db/models/query_utils.py index d566cc1883..6e82edb6e7 100644 --- a/django/db/models/query_utils.py +++ b/django/db/models/query_utils.py @@ -285,14 +285,16 @@ def select_related_descend(field, restricted, requested, load_fields, reverse=Fa return False if not restricted and field.null: return False - if load_fields: - if field.attname not in load_fields: - if restricted and field.name in requested: - msg = ( - "Field %s.%s cannot be both deferred and traversed using " - "select_related at the same time." - ) % (field.model._meta.object_name, field.name) - raise FieldError(msg) + if ( + restricted + and load_fields + and field.name in requested + and field.attname not in load_fields + ): + raise FieldError( + f"Field {field.model._meta.object_name}.{field.name} cannot be both " + "deferred and traversed using select_related at the same time." + ) return True