From 2ec240436034b567ccfa09901e8ade2457bc1b60 Mon Sep 17 00:00:00 2001 From: ekaplan1 Date: Wed, 25 Oct 2023 11:05:02 -0400 Subject: [PATCH] Improved support for accessing models when using multiple databases --- polymorphic/models.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/polymorphic/models.py b/polymorphic/models.py index df0cc485..af069561 100644 --- a/polymorphic/models.py +++ b/polymorphic/models.py @@ -200,7 +200,8 @@ def __init__(self, *args, **kwargs): def create_accessor_function_for_model(model, accessor_name): def accessor_function(self): objects = getattr(model, "_base_objects", model.objects) - attr = objects.get(pk=self.pk) + using = kwargs.get("using", self._state.db or DEFAULT_DB_ALIAS) + attr = objects.using(using).get(pk=self.pk) return attr return accessor_function @@ -269,3 +270,4 @@ def add_all_sub_models(super_cls, result): add_all_super_models(self.__class__, result) add_all_sub_models(self.__class__, result) return result +