From 7b0df38ff157eab404460972dcea211103f7d77b Mon Sep 17 00:00:00 2001 From: GuySharir Date: Mon, 8 Apr 2024 17:45:36 -0700 Subject: [PATCH 1/2] failing test --- django_multitenant/tests/test_models.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/django_multitenant/tests/test_models.py b/django_multitenant/tests/test_models.py index 0fb0240..8a80f8b 100644 --- a/django_multitenant/tests/test_models.py +++ b/django_multitenant/tests/test_models.py @@ -150,6 +150,15 @@ def test_filter_select_related_not_id_field(self): in captured_queries.captured_queries[0]["sql"] ) + def test_related_manager_query(self): + from .models import Project + + account = self.account_fr + Project.objects.create(account=account, name=f"test_project") + + with self.assertNumQueries(1): + list(account.projects.all()) + def test_prefetch_related(self): from .models import Project From 1b161a9f938584617659ba824e45ceee766abf97 Mon Sep 17 00:00:00 2001 From: GuySharir Date: Mon, 8 Apr 2024 17:46:20 -0700 Subject: [PATCH 2/2] fix is_val_equal_to_tenant --- django_multitenant/mixins.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/django_multitenant/mixins.py b/django_multitenant/mixins.py index e42d3ba..7c54cd8 100644 --- a/django_multitenant/mixins.py +++ b/django_multitenant/mixins.py @@ -2,6 +2,7 @@ from django.db.models.sql import DeleteQuery, UpdateQuery from django.db.models.deletion import Collector +from django.db.models import Model as DjangoModel from django.db.utils import NotSupportedError from django.conf import settings @@ -126,12 +127,13 @@ def __setattr__(self, attrname, val): # Provides failing of the save operation if the tenant_id is changed. # try_update_tenant is being checked inside save method and if it is true, it will raise an exception. def is_val_equal_to_tenant(val): - return ( - val - and self.tenant_value - and val != self.tenant_value - and val != self.tenant_object - ) + is_equal = val and self.tenant_value and val != self.tenant_value + + if is_equal and isinstance(val, DjangoModel): + field = get_tenant_field(val) + is_equal = getattr(val, field.name) != self.tenant_value + + return is_equal if ( attrname in (self.tenant_field, get_tenant_field(self).name)