From 99f685a7f006b2d146b5ae403d350c6351576399 Mon Sep 17 00:00:00 2001 From: Andy Babic Date: Sun, 4 Feb 2024 10:31:00 +0000 Subject: [PATCH] Update get_model_field() to raise a clear error when relationship traversal isn't possible --- modelcluster/utils.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/modelcluster/utils.py b/modelcluster/utils.py index ae94240..80afb00 100644 --- a/modelcluster/utils.py +++ b/modelcluster/utils.py @@ -66,9 +66,18 @@ def get_model_field(model, name): subject_model=subject_model, ) ) - if hasattr(field, "related_model"): + if getattr(field, "related_model", None): traversals.append(TraversedRelationship(subject_model, field)) subject_model = field.related_model + else: + raise FieldDoesNotExist( + "Failed attempting to traverse from {from_field} (a {from_field_type}) to '{to_field}'." + .format( + from_field=subject_model._meta.label + '.' + field.name, + from_field_type=type(field), + to_field=field_name, + ) + ) try: field = subject_model._meta.get_field(field_name) except FieldDoesNotExist: