from django.db import models
from django_multitenant import set_current_tenant, TenantModel
class Tenant(models.Model)
pass
class Bar(TenantModel):
tenant = models.ForeignKey(Tenant)
tenant_id = 'tenant'
class Foo(TenantModel):
tenant = models.ForeignKey(Tenant)
tenant_id = 'tenant'
bar = models.ForeignKey(Bar)
tenant = Tenant.objects.create()
bar = Bar.objects.create(tenant=tenant)
foo = Foo.objects.create(tenant=tenant, bar=bar)
set_current_tenant(tenant)
print(Foo.objects.get().bar)
...will output this SQL:
SELECT "foo"."id", "foo"."tenant_id", "foo"."bar_id" FROM "foo" WHERE "foo"."tenant_id" = '3d340b31-cd78-4224-9ac0-00455933c1b8'::uuid
SELECT "bar"."id", "bar"."tenant_id" FROM "bar" WHERE "bar"."id" = 2
The second query there should have an additional AND "bar"."tenant_id" = ... in the WHERE clause
Changing TenantModel to include:
class TenantModel:
class Meta:
base_manager_name = 'tenantManager'
tenantManager = TenantManager()
The following:
...will output this SQL:
The second query there should have an additional
AND "bar"."tenant_id" = ...
in the WHERE clauseChanging TenantModel to include:
seems to fix the issue