From 18adec92d834c9cb3a45c87b6f67625c0d225005 Mon Sep 17 00:00:00 2001 From: "@whiskeyriver" Date: Wed, 17 May 2023 01:48:27 -0700 Subject: [PATCH] Call property mapper to materialize lazy attrs A Relationship property from a mapper may not have all expected attributes available immediately. Calling the `prop.mapper` property will cause these missing attributes to materialize. --- flask_admin/contrib/sqla/form.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/flask_admin/contrib/sqla/form.py b/flask_admin/contrib/sqla/form.py index bd99744ac..3e694a086 100644 --- a/flask_admin/contrib/sqla/form.py +++ b/flask_admin/contrib/sqla/form.py @@ -670,6 +670,13 @@ def _calculate_mapping_key_pair(self, model, info): reverse_prop = None for prop in target_mapper.iterate_properties: + try: + # Loading the mapper property will populate a lazy direction + # attribute + prop.mapper + except AttributeError: + continue + if hasattr(prop, 'direction') and prop.direction.name in ('MANYTOONE', 'MANYTOMANY'): if issubclass(model, prop.mapper.class_): reverse_prop = prop