citusdata / django-multitenant

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

Trouble getting started with DRF - No filtering, and error message on getattr #91

Closed florianmartens closed 1 year ago

florianmartens commented 3 years ago

Thank you for providing this library for free. I think it is very valuable. I'm struggling to get the library to work for my setup, though.

I created a tenant and a user model, the object I want to filter is a tag model:

class Tenant(TenantModal):
    tenant_id = 'id'
    name = models.CharField(max_length=255)

class CustomUser(AbstractBaseUser, TenantModel):
    tenant_id = "tenant_id"
    uuid = models.UUIDField(
        primary_key=True, default=uuid.uuid4, editable=False)
    tenant = TenantForeignKey('tenants.Tenant', blank=True, null=True, on_delete=models.CASCADE)

    objects = CustomUserManager()

    USERNAME_FIELD = 'email'

class TenantTag(TenantModel):

    tenant_id = "tenant_id"
    uuid = models.UUIDField(primary_key=True, editable=False, default=uuid.uuid4)
    tenant = TenantForeignKey(Tenant, on_delete=models.CASCADE)

In this setup, I've tried to use a normal ForeignKey (as shown in the docs) and a TenantForeignKey (as shown here). Both resulted in the following 2 problems:

1.) Whenever I want to save an instance (via the admin) I receive an error message: File "/Users/xxxxxxxx/.virtualenvs/hr-djang/lib/python3.7/site-packages/django_multitenant/mixins.py", line 84, in tenant_value return getattr(self, self.tenant_field, None) TypeError: getattr(): attribute name must be string

2.) I can not get the models to be filtered for tenants in any way. Even though the tenants seem correctly set by the middleware and I'm able to get the tenant by invoking get_current_tenant()no filtering is applied. Sending an authenticated request from a tenant_1_user shows tags belonging to tenant_1 and tenant_2 as well.

I would really appreciate any help! Thanks.

pickyuptruck commented 3 years ago

Have you tried the recommendation in #7 ?

Also your tenanted models only require a ForeignKey to the tenant model

class TenantTag(TenantModel):

    tenant_id = "tenant_id"
    uuid = models.UUIDField(primary_key=True, editable=False, default=uuid.uuid4)
    **tenant = ForeignKey(Tenant, on_delete=models.CASCADE)**
louiseGrandjonc commented 3 years ago

Hi @florianmartens,

I'm not sure where the issue is. I was wondering what engine you were using in your DATABASES settings. Did you change it to 'ENGINE': 'django_multitenant.backends.postgresql', ?

gurkanindibay commented 1 year ago

@florianmartens are you using the django_multitenant.backends.postgresql setting as @louiseGrandjonc suggested? Do you still have the problem?

gurkanindibay commented 1 year ago

This issue does not seem to be a problem. I'm closing right now. If you have further issues, please open the issue and add some details.