citusdata / django-multitenant

Python/Django support for distributed multi-tenant databases like Postgres+Citus
MIT License
721 stars 119 forks source link

Tenant pk field hardcoded as 'id' #24

Closed agustindidiego closed 5 years ago

agustindidiego commented 6 years ago

TenantModel provides tenant_id attribute to specify the pk field to be used, or at least, the examples points out to specify tenant_id = 'id' to the Store model, which is used as the Tenant model. But if I try to specify a different pk column to the Tenant model I got several errors because TenantManager and TenantQuerySet refers to current_tenant.id

This should probably be getattr(current_tenant, current_tenant.tenant_id)

#Below is the manager related to the above class. 
class TenantManager(TenantQuerySet.as_manager().__class__):
    #Injecting tenant_id filters in the get_queryset.
    #Injects tenant_id filter on the current model for all the non-join/join queries. 
    def get_queryset(self):
        current_tenant=get_current_tenant()
        if current_tenant:
            kwargs = { self.model.tenant_id: getattr(current_tenant, current_tenant.tenant_id)}
            return super(TenantManager, self).get_queryset().filter(**kwargs)
        return super(TenantManager, self).get_queryset()